2

I've been trying to get Audit.Net to work for my MVC EF 6.2 project. I have everything set up and is able to save my audit logs into my sql server table. The issue that I have is for objects that are directly being returned from my db context, EF creates a dynamic proxy type for such objects and for some reason when used as the target object in my audit scope, it just hangs at the using statement without ever going into the using block and no exception/errors shows up. Just hangs there and nothing happens.

Everything works for objects that are being returned from my webapi/wcf services as a DTO.

My setup in Global.asax

 Audit.Core.Configuration.Setup()
    .UseSqlServer(config => config

    .ConnectionString(ConfigurationManager.ConnectionStrings["DBContext"].ConnectionString)
                .Schema("dbo")
                .TableName("SystemEvents")
                .IdColumnName("EventId")
                .JsonColumnName("Data")
                .LastUpdatedColumnName("LastUpdatedDate"));

Code for audit scope:

var record = _context.FileRecord.Include(a => a.Contact).SingleOrDefault(c => c.Id == id);
using(var audit = AuditScope.Create(new AuditScopeOptions 
{ DataProvider = new SqlDataProvider { // settings here }, 
  EventType = "Update", 
  TargetGetter = () => FileRecord 
}) <--- hangs right here and never executes the code inside the brackets
{
     record.ContactId = 33
     // more properties to be updated
}

_context.SaveChanges();

My table model:

public class FileRecord
{
    public int Id { get; set; }
    // more members

    // Have references to other tables here
    public virtual ICollection<StageHistory> StageHistories { get; set; }
    public virtual Contact Contact { get; set; }
}

This seems to be an issue with EF creating proxy types because I am able to get this to work if I just set the target object to either null or create a completely new instance of the object like () => new FileRecord() to get the actual entity object and not the proxied version, but doing so I won't be able to keep track of the old and new changes on the object.

Any help would be appreciated, thanks.

Ottarl
  • 47
  • 7
  • Why don't you use the [`Audit.EntityFramework`](https://github.com/thepirat000/Audit.NET/blob/master/src/Audit.EntityFramework/README.md) extension that automatically create the audit event with the changes for every EF `SaveChanges()` call? – thepirat000 Oct 19 '19 at 16:52
  • I was unable to reproduce (using EF 6.2) Please check [this screenshot](https://imgur.com/Iej0ozL). I guess the problem is with the serialization of your FileRecord. – thepirat000 Oct 19 '19 at 17:14
  • @thepirat000 I am using the Audit.EntityFramework extension but I also want to save audits from certain functions into another table. Sorry I left out part of my code that I have no edited. I'll also take a closer look at my FileRecord and see what might be the problem is, might be an issue with circular reference? – Ottarl Oct 21 '19 at 16:03
  • Yes it could be, but the default JSON serialization already handles the circular references. Check [Configuration.cs](https://github.com/thepirat000/Audit.NET/blob/308461fd5a50346366e496d1f4503d3af4759fae/src/Audit.NET/Configuration.cs#L45) – thepirat000 Oct 21 '19 at 16:40

0 Answers0