Entity Framework 4 not respecting database constraints for numeric fields

久未见 提交于 2019-12-11 18:08:35

问题


Say, I have a table in the DB defined as follows

Table: Foo
PkId - int, primary, autoincrement
Bar - int, allow null=false, no default

Now when generating the EF model from the database the 'Bar' field is correctly defined as Nullable=false, Type=Int32.

Now when I do the following

var foo = new Foo();  
context.AddToFoos(foo);  
context.SaveChanges();

The row is inserted into the database and 'Bar' has a value of 0? I would have expected an exception because Bar hasn't been set. I realise that 0 isn't null but its also not a value that I've set.

Is this by design or have I misunderstood something?


回答1:


It's not nullable and thus an int. The default of int is 0. So the DB is happy and the framework is fine as well.



来源:https://stackoverflow.com/questions/6111251/entity-framework-4-not-respecting-database-constraints-for-numeric-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!