开发者

Entity Framework 4 not respecting database constraints for numeric fields

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 'B开发者_开发问答ar' 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜