question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Audit EF is saving an Inserted action when no insert actually happened.

See original GitHub issue

Full reproduction: https://github.com/VictorioBerra/Audit.NET_EF_Issue200_RelatedEntitys

There are a few important things to note. One, is the automapper profile:

cfg.CreateMap<CatUpdateViewModel, Cat>()
    .ForMember(dest => dest.CatBreedLine,
        opts => opts.MapFrom(src => src.CatBreedIds.Select(id => new CatBreedLine()
        {
            CatId = id,
            CatBreedId = src.Id
        })));

You can see, for every CatUpdateViewModel that I map to a Cat entity, it creates new instances of CatBreedLine.

Anyways, if you run the sample, look at the console output, notice no second insert for a CatBreedLine happened, yet Audit.EF saves an Insert audit record!!

image

image

Also, I would like EF to NOT bump the Created*/Updated* stuff for the CatBreedLine too since the record didnt technically ever change…

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
thepirat000commented, Feb 13, 2019

I’m closing this since it doesn’t seem to be considered as an issue for EF, it’s more a design decision and I don’t think this behavior is going to change.

So for your case, you should probably Include() the CatBreedLine on the Cat retrieval and have some logic instead of the VM->Cat mapping that overrides the lines.

0reactions
VictorioBerracommented, Feb 13, 2019

Unfortunately, my Cat example was a huge simplification of a much bigger project with many relationships and entities and generic repos 😬 so it wont be that easy. But i appreciate the advice.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot Audit on Insert Entity Framework.I am stuck can you ...
Once you've audited the item, set the key (bool) to true, to signify it has been audited. Or another option is to instead...
Read more >
How To Track Entity Changes With EF Core | Audit Logging
0:30 IAuditableEntity 2:30 Using EF interceptors for auditing 6:09 Configuring the interceptor 7:27 Auditing in action.
Read more >
Interceptors - EF Core
The result is a SaveChangesAudit entity with a collection of EntityAudit entities, one for each insert, update, or delete. The interceptor then ...
Read more >
Audit Trail Implementation in ASP.NET Core with Entity ...
I will create a very basic CRUD Application using .NET 5 MVC, Microsoft Identity, and Entity Framework Core. Once this is done, we...
Read more >
Entity Framework - Get identity value in SavinChanges event
Note The identity column value will be returned only after the inserted statement committed successfully. 1. Insert the Entity Object. 2. Save ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found