Entity Framework 4 Partialy SaveChanges

隐身守侯 提交于 2019-12-24 06:47:44

问题


Lets say i have this:

var entity = db.histories.GetWhere(x => x.Body == "MyBody").FirstOrDefault();
var entity2 = db.histories.GetWhere(x => x.Body == "MyBody2").FirstOrDefault();
        entity.From = "lmao!";
        entity2.From = "lmao2!";

now i know that to update i have to call db.SaveChanges();

my question is what if i want to update entity only and not entity2 ?

is that even possible ? could be simple im not sure.

thanks in advance.


回答1:


Either get the 2 entities from separate contexts:

var entity = db.histories.GetWhere(x => x.Body == "MyBody").FirstOrDefault();
var entity2 = differentDbInstance.histories.GetWhere(x => x.Body == "MyBody2").FirstOrDefault();        

or retrieve from the same context but detach before making changes you dont want saved

db.Detach(entity2);
entity2.From = "lmao2!";

The latter is better design but you may need to former depending on the scenario




回答2:


This has been asked before, and no, there is no way to accomplish this.

entity and entity2 would have to be on different data contexts to achieve what you're looking for.



来源:https://stackoverflow.com/questions/6415291/entity-framework-4-partialy-savechanges

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