After loading an entity using no tracking option can I enable tracking for that entity again?
Thanks
Related
I have a service that use EF 6.1.3 to access to the database. I have POCO entities to store the results of EF, but the resutls are dynamicProxies instead of the POCO entity.
The problem is that when the service try to send to the client this dynamic proxy, I get an error receiving the http response.
I have tried to disabled the creation of the proxies entities in my dbContext, and then I receive my real POCO entity so I have no problems.
But really I don't know what is dynamic proxies and when is usuful to use them and when I can disabled them.
EDIT: I have disabled lazy loading.
Thanks.
When creating instances of POCO entity types, the Entity Framework
often creates instances of a dynamically generated derived type that
acts as a proxy for the entity. This proxy overrides some virtual
properties of the entity to insert hooks for performing actions
automatically when the property is accessed. For example, this
mechanism is used to support lazy loading of relationships.
Source: https://msdn.microsoft.com/en-us/data/jj592886.aspx
You will find everything you need in the above article!
I used the EF 5.x DbContext Fluent Generator to generate my POCO classes but my properties are not coded as virtual. Don't you have to have that for tracking to occur? Why wouldn't the template already use virtual for properties?
Because we found that for the majority of users it was better to use snapshot change tracking rather than change tracking proxies. Change tracking proxies have their place in certain situations, but usually they add complexity without any real benefit. For more info see http://blog.oneunicorn.com/2011/11/24/why-are-the-dbcontext-t4-templates-so-different-from-the-ef4-poco-templates/ and http://blog.oneunicorn.com/2011/12/05/should-you-use-entity-framework-change-tracking-proxies/
Can anyone direct me to a good strategy for implementing change tracking in my Entity Framework model?
I have around 20 entities to track changes on (accessed via facades / unit of work) and I need to be able to display who changed what when on displaying the record in the UI.
I know there's Context.OnSavingChanges (or whatever it's called) but I'd probably want to access the changes in queries like context.MyEntity.ChangeLog
Must I create a ChangeLog entity, add associations to all the entities or is there a better via via savingchanges?
Richard
P.s. Have a great weekend!
Entity framework is ORM = API responsible for persistence and loading from database. What you persist or load is completely up to you so if you want change tracking you must to code it.
The most common approach is indeed using OnSavingChanges or overriding SaveChanges because you are usually saving changed executed by single user.
An old question but for anyone looking for auditing changes on EF >= 6 or EF Core, I worked on an open source library Audit.EntityFramework you could try.
See FrameLog, which is an open source library I wrote for this purpose. You call it from SaveChanges and it deals with the rest, including giving you a strongly-typed API for querying the logs.
I am reading this E.F. Team Blog's this series http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-1-introduction-and-model.aspx
At many places i saw this term "Entity Tracked by Context", specially in this part http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx (part4)
My questions are
What does "Entity Tracked by Context" mean??
Does same context is used for every request or new context is created for each request (I am using E.F. for Asp.Net MVC APP)
Tracking every entity (loaded) must be adding some memory-overhead on server, is it??
"Entity Tracked by Context" mean that context is aware of the entity, it knows state of the entity and changes made to the entity. Context can work only with tracked entities. If you call save changes only changes on tracked entities will be persisted to the database. Tracked and Attached can be considered as synonyms.
In EF we are usually talking about attached entities and detached entities. Attached entities are tracked by the context. Entity become attached if you load it from database (unless you manually for EF to don't track the entity) or if you call Attach or Add (DbContext API) / AddObject (ObjectContext API) for the entity. You can force entity to detach from the context either by calling Detach (ObjectContext API) or setting the state to Detached (DbContext API). If you just create the entity in your code as any other class it is considered as detached until you call Attach for it.
New context is always created for each request - web application works a lot with detached entities. That will also solve the issue with memory. If you detach all entities you want to store in some state (like session) and if you dispose the context correctly in each request you will free the memory.
It is possible to use the Entity framework caching and tracing framework with the new entity framework 4.1 code first? If so then is there a good example of this, otherwise what is the best way to cache some queries?
Here is an example of a caching system for EntityFramework (with EDMX) http://code.msdn.microsoft.com/EFProviderWrappers-c0b88f32
Here is how you use that example with CodeFirst
http://jkowalski.com/2010/04/23/logging-sql-statements-in-entity-frameworkcode-first/
Although he is demoing the tracing provider rather than the caching provider you can use the same principals