如何让 EF 理解某些列不可为空?
How to make EF understand that some columns are not nullable?
我已经定义好了table。
create table WeirdDonkey (
Tails int not null,
Heads int default 7
)
当我在上面运行 EF时,它会创建如下两个字段。
public int Tails { get; set; }
public Nullable<int> Heads { get; set; }
现在,我明白为什么它认为头的数量可以为空,但它没有任何意义。驴子不能有 null 个头。如果没有指定,它可以有 7 个。 (好吧,从动物学上讲这没有任何意义,但让我们假设我们在另一个遵循我的 SQL 魔法规则的星球上)。
如何让 EF 明白默认值为 7 并且不会有空头驴?
EF 是对的。 Heads
可以有空值。如果在插入一行后,您决定将 Heads
值更新为 null
,会发生什么情况。数据库会不会接受?是的。
如果您不想要空值,则必须这样明确定义它:
create table WeirdDonkey (
Tails int not null,
Heads int not null default 7
)
我已经定义好了table。
create table WeirdDonkey (
Tails int not null,
Heads int default 7
)
当我在上面运行 EF时,它会创建如下两个字段。
public int Tails { get; set; }
public Nullable<int> Heads { get; set; }
现在,我明白为什么它认为头的数量可以为空,但它没有任何意义。驴子不能有 null 个头。如果没有指定,它可以有 7 个。 (好吧,从动物学上讲这没有任何意义,但让我们假设我们在另一个遵循我的 SQL 魔法规则的星球上)。
如何让 EF 明白默认值为 7 并且不会有空头驴?
EF 是对的。 Heads
可以有空值。如果在插入一行后,您决定将 Heads
值更新为 null
,会发生什么情况。数据库会不会接受?是的。
如果您不想要空值,则必须这样明确定义它:
create table WeirdDonkey (
Tails int not null,
Heads int not null default 7
)