开发者

Can Entity Framework's database or model go into an inconsistent state?

I'm building an application using EF 4.1 and I was wondering if the underlying database serving the EF application can go into an inconsistent state. What I mean is, in case of an excepti开发者_JAVA技巧on during the creation/modification/deletion of entities, can the EF be corrupted and leave some values that shouldn't be null as null and save some part of the database. (especially in case of an exception in the EF context).

Let me give an example: I've got two independent entity types, A and B, and A and B has a 1-1 association. I create the object A, and during creation of B, there is a problem and it raises an exception. Afterwards, context's SaveChanges method is called anyway. (this is only one example, there can be other association types, inheritence hiearchies etc) Is a corruption likely to occur, or is there a layer that will prevent inconsistency. There definitely should be, with SQL server constraints and null checking. But I start the database, and sometimes, I get an exception telling that something (association) that cannot be null as defined as not nullable, is actually null, and as I haven't handled that value being null (imagine with the previous example, the reference A.B is being null), my app/site crashes. What could be the reason? There are no other applications or instances accessing the database in any way. These kinds of corner cases have been happening to me lately, and I was wondering the underlying reason.


DbContext.SaveChanges() wraps the generated SQL statements in a transaction. So either eveything gets committed or it's being rolled back. You might want to check the relationships and constraints in your database. Also usimg the Sql Server profiler to see what actually gets sent to your DB can be helpful.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜