开发者

When to call Context.SaveChanges

I am using Entity Framework. One of my Entities (MyEntity) has a dependency on a "key" that I get by executing a stored proceure which lives outside of the scope of MyEntity (As a matter of fact it doesn't even exist in the same Context, its a call to a seperate assembly). once I get this "key" I perform some other actions as well (essentially setting other properties of MyEntity) then I call the context.SaveChanges() to save the changes.

My question is ... is it ok to call Context.SaveChanges() twice? Once I have the "key开发者_高级运维" and once again after I set other properties of MyEntity? The reason I ask is that once I have the "key", I must tie it with the current MyEntity instance I am working with otherwise I will end up with duplicates/orphans. And if something happens and other properties of MyEntity object are not saved, well its not a fatal issue.

Yes, I know, ideally this would be done in one transaction but we dont always have that luxury :(

Thanks!


I don't see a problem with that. But you'll have to remember to set the myEntity.State = EntityState.Modified after the first save.


Ignoring the problems with this system that you yourself have noted, there should be no problem calling SaveChanges() twice.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜