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.