Example: a product entity is loaded including its tags without tracking:
repository.Product
.Include("Tag")
.Where(p => p.ProductID == 1)
.Execute(MergeOption.NoTracking);
Note that this is a many-to-many relationship; a product can have several tags, and tags can be associated with several products.
Somewhere else, I want to save any changes made to the product entity, but without saving changes made to its related tags or its relationship with these tags.
Meaning, neither of these changes may be saved:
- A tag has been removed from the product
- A tag has been added to the product
- A tag has been modified (e.g. name has been changed)
So I was thinking that I could somehow attach only the product to a new ObjectContext and save changes. But for some reason I can't figure out how to only attach a single entity to the object context, and not the entire graph.
Of course, I could attach the graph and then manually detach all other entities except the produ开发者_如何转开发ct in question, but this is a horrible solution, and I was hoping to find another.
You can try to make a clone of your Product (not deep clone!), attach the clone and save changes. Your original object graph will remain detached. The only problem can be if you are using something like timestamp for concurrency handling. You will have to copy new timestamp from clone back to original entity otherwise you will not be able to save original entity again.
精彩评论