简体   繁体   中英

How to track which entities are updated in Entity Framework

I am working on an eCommerce based web application with MVC and Entity Framework. I just want to track/know which entities are updated. For example, when the user changes the stock and price for any product and clicks the Save button, I want to track that these two entities are updated with their ProductId .

Is there any way to achieve this?

Thanks.

Disclaimer : I'm the owner of the project Entity Framework Plus

This project allows to audit / track everything what's is modified. There is a lot of options available if you only want to access modified entities.

// using Z.EntityFramework.Plus; // Don't forget to include this.

var ctx = new EntityContext();
// ... ctx changes ...

var audit = new Audit();
audit.CreatedBy = "ZZZ Projects"; // Optional
ctx.SaveChanges(audit);

// Access to all auditing information
var entries = audit.Entries;
foreach(var entry in entries)
{
    foreach(var property in entry.Properties)
    {
    }
}

Documentation: EF+ Audit

NuGet: https://www.nuget.org/packages/Z.EntityFramework.Plus.EF6/

Yes you can with this

//let 'myObj' is entity instance
//let db is your context instance
if ( db.Entry(myObj).State == System.Data.EntityState.Modified )
{
 //Try your code here
}

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