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.

Entity Framework - ObjectContext SaveChanges not being caught

See original GitHub issue

Describe the bug ObjectContext SaveChanges not being caught for Updating of an audited entity. **EDIT - This seems to being able to detect any changes for some reason when saving with ObjectContext.

To Reproduce Contextual save - called from a service project within master project.

var objContext = ((IObjectContextAdapter)_dbContext).ObjectContext;
_dbContext.Entry(user).State = EntityState.Modified;
objContext.SaveChanges();

Data provider:

using Audit.Core;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TMS.Util
{
    public class PulseDataProvider : AuditDataProvider
    {
        public override object InsertEvent(AuditEvent auditEvent)
        {
            var fileName = $"Log{Guid.NewGuid()}.json";
            File.WriteAllText(@"D:\Logs\" + fileName, auditEvent.ToJson());
            return fileName;
        }
        public override void ReplaceEvent(object eventId, AuditEvent auditEvent)
        {
            var fileName = eventId.ToString();
            File.WriteAllText(@"D:\Logs\" + fileName, auditEvent.ToJson());
        }
        public override T GetEvent<T>(object eventId)
        {
            var fileName = eventId.ToString();
            return JsonConvert.DeserializeObject<T>(File.ReadAllText(fileName));
        }
    }
}

DB context

[AuditDbContext(Mode = AuditOptionMode.OptIn, AuditEventType = "{database}_{context}", ExcludeValidationResults = true, IncludeIndependantAssociations = true)]
    public class DBContext : AuditIdentityDbContext<User, Role, int, UserLogin, UserRole, UserClaim>, IObjectContextAdapter

Startup

Audit.Core.Configuration.Setup()
                .UseCustomProvider(new PulseDataProvider());
            Audit.EntityFramework.Configuration.Setup()
                .ForAnyContext(config => config
                    .IncludeIndependantAssociations()
                    .IncludeEntityObjects());`

Expected behavior Excepting the creation of an entry within my data provider.

Libraries (specify the Audit.NET extensions being used including version): For example:

  • Audit.EntityFramework: 16.2.0.0

Target .NET framework: For example:

  • .NET Standard 2.1
  • .NET Framework 4.7.2
  • EntityFramework 6.4.4

Additional context This issue only seems to be when executing a savechanges on a objectcontext level and works fine when executed on DBContext level.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
alexcherkacommented, Dec 22, 2020

I took your approach and it worked flawlessly! I’m going to go ahead and close the ticket, its working perfectly and exactly as intended! Thanks for the help @thepirat000 !

1reaction
alexcherkacommented, Dec 17, 2020

You got it exactly correct, I actually has transitioned to the non inheritance version today earlier and was just about to do what you suggested. It works flawlessly, A few minor quirks(thinking its the basis of my program design) to work out, Ill be testing with this extensively over the next week, so if I find any issues ill report them here! Awesome work and thanks for the help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot SaveChanges() with DbContext Entity Framework ...
I have reverse engineered all the models using EF Core successfully, but upon trying to add new items to the database using my...
Read more >
ObjectContext.SaveChanges Method (System.Data.Objects)
You can resolve an optimistic concurrency violation by catching it, calling the Refresh method with the StoreWins or ClientWins value, and then calling ......
Read more >
8 Entity Framework Gotchas
ObjectContext Does Not Track Changes Naturally in Web Apps. The Visual Studio compiler does not alert you when you are using bad patterns....
Read more >
Entity Change Tracking using DbContext in ...
When the call to SaveChanges is made, Entity Framework is aware of what values have changed, what the new values are, and what...
Read more >
Exception during DbContext.SaveChanges()
My primary goal is to get rid of this code, to be able to make changes to the DB schema again. The second...
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