简体   繁体   中英

c# entity framework track dirty on entity not added to context yet

I suppose the question is simple enough. I am doing something where I am creating the entity and then not immediately adding it to the context meaning its officially not going to be tracked for changes.

Example:

Create entity: var g = new Foo(){id = 1, description = "Object1"} (Upon creation this object does not trigger any changed or dirty flags)

Change property: g.description = "Object2"; (At this point I would like for something to say it is dirty)

The one thing i can think of would be to somehow create the object then add it to the context, but then immediately roll back the contexts tracking of the fact that an object was just added and then any further changes to that object would trigger the Context.changeTracker.HasChanges() to trigger.

Is this possible?

Either use INotifyPropertyChanged and create a OnPropertyChanged function that executes the event and also remmembers all the changed properties in a list.

Or you could use something more complex like AOP. An example of that is to use Castle's Dynamic Proxy: http://www.christophdebaene.com/blog/2009/01/10/aop-in-action-part-2-dirty-tracking-using-mixins-with-castles-dynamicproxy/

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