F# EFCore 中的 varbinary 列使用什么可空类型?

What nullable type to use for varbinary column in F# EFCore?

我有一个 MSSQL 数据库列,它是一个可为空的 varbinary(8)。当我尝试使我的 F# 记录中的字段可为空时,出现以下错误:

"A generic construct requires that the type 'byte[]' have a public default constructor"

Nullable<byte[]>Nullable<Byte[]>Nullable<Byte list>Nullable<seq<byte>> 均未超出此错误。

为简洁起见,我省略了其他字段。

[<Table("SurveyResponse"); CLIMutable>]
type SurveyResponse = {
    mutable ContactId: Nullable<byte[]>
}

是否有我可以使用的可空 F# 类型,EF Core 会正确转换为 varbinary(8)?

我没有太多使用 EF 的经验,也没有尝试此操作的所有设置,但您收到的错误消息来自 F# 类型检查器,而不是来自 EF。

问题是 Nullable<'T> 要求类型 'T 是值类型 - Nullable 的目的是将 null 添加到其他类型不将 null 作为值,例如 intfloat。但是,数组本身可以是 null,因此不需要 Nullable

所以,假设 EF 在这里没有做任何特别的事情,我认为以下应该可以解决问题:

[<Table("SurveyResponse"); CLIMutable>]
type SurveyResponse = {
    mutable ContactId: byte[]
}

给定 SurveyResponsesr,您可以检查 sr.ContactId <> null 来处理 null 值。