简体   繁体   中英

Entity Framework 4 Partialy SaveChanges

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM