Entity Framework - ObjectContext SaveChanges not being caught
See original GitHub issueDescribe 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:
- Created 3 years ago
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 !
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!