实体框架4不考虑数据库对数字字段的约束

问题描述

| 说,我在数据库中有一个表,定义如下 表:Foo PkId-int,primary,自动递增 Bar-int,允许null = false,无默认 现在,当从数据库生成EF模型时,\'Bar \'字段正确定义为Nullable = false,Type = Int32。 现在,当我执行以下操作时
var foo = new Foo();  
context.AddToFoos(foo);  
context.SaveChanges();
该行插入到数据库中,并且'Bar'的值为0?由于没有设置Bar,我本来会期待一个例外。我意识到0不为空,但也不是我设置的值。 这是设计使然还是我误解了?     

解决方法

它不能为null,因此为int。 int的默认值为0。因此,数据库很满意,框架也很好。     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...