Audit.NET icon indicating copy to clipboard operation
Audit.NET copied to clipboard

Audit.EntityFramework - back to issue #53

Open mdrewanz opened this issue 9 years ago • 4 comments

Hi!, With regards the solution proposed to have the context tracking and show the before, after values - it works for a simple entity like the one discussed in the issue.

I'm having the same problem as described by issue # 53 on a complex entity where the update is done by the use of a IObjectState to track object state (Added, Modified, Removed) which is translated to entity state just before calling SaveChanges.

As this is used on a disconnected scenario, all updates are transferred from a model to a fresh copy of the entity retrieved from the database, marking the IObjectState with the appropriated status.

However, when updating the whole graph, Entity Framework and Audit are not able to catch the changes accordingly.

Any hint on how to address this?

Regards, MD

mdrewanz avatar Aug 24 '17 02:08 mdrewanz

Can you share some code to reproduce this?

What do you do before the SaveChanges call?

thepirat000 avatar Aug 27 '17 02:08 thepirat000

Hi, Sorry taking so long as I had to build a sample to demo what's happening. We're using the pattern described by Julie Lerman to address updating graphs on disconnected scenarios. In the sample, We've managed to test only Audit.NET with AuditScope and Audit.EntityFramework. The second does not capture the changes whilst the first reflects all the changes made to original data.

The sample project is here. It's a quick and dirty code (and incomplete) but shows up the thing. We've kept all the comments to show where things happen. Right after creating an order, we could Edit / Save (update is happening internally) and see the audit results.

Right before SaveChanges we have the following code


context.Configuration.AutoDetectChangesEnabled = false;

foreach (var item in order.Items)
 {
 context.OrderItems.Attach(item);
 }

context.ChangeTracker.DetectChanges();
context.FixState();
context.SaveChanges();

Any insights on how to fix this will be appreciated.

Regards.

mdrewanz avatar Aug 30 '17 22:08 mdrewanz

@thepirat000 do you have a working sample for Entity framework ?

UsamaKhalil-KSO avatar Sep 11 '17 04:09 UsamaKhalil-KSO

There are many Unit Tests for EntityFramework, i.e.: https://github.com/thepirat000/Audit.NET/blob/master/test/Audit.EntityFramework.UnitTest/EfTests.cs

thepirat000 avatar Sep 11 '17 04:09 thepirat000