Search code examples
entity-frameworkaudit.net

How To Save Entity Relations With Audit.Net?


I am using Audit.Net (Audit.EntityFramework) and I want to know how can I save an entity's relation?

Here's my configuration

Audit.Core.Configuration.Setup()
                .UseEntityFramework(x => x
                    .AuditTypeMapper(typeName => typeof(AuditLog))
                    .AuditEntityAction<AuditLog>((ev, ent, auditEntity) =>
                    {
                        auditEntity.Table = ent.Table;
                        auditEntity.AuditDate = DateTime.UtcNow;
                        auditEntity.Action = ent.Action;
                        auditEntity._Changes = ent.Changes;
                        auditEntity._Entries = ev.GetEntityFrameworkEvent().Entries;
                        auditEntity.Success = ev.GetEntityFrameworkEvent().Success;
                        auditEntity._ColumnValues = ent.ColumnValues;
                        auditEntity._PrimaryKey = ent.PrimaryKey;
                    }));

Consider the following relationship

public class Blog
{
    public int Id { set; get; }
    public string Title { set; get; }
    public string AuthorName { set; get; }

    public IList<Post> Posts { set; get; }
}

public class Post
{
    public int Id { set; get; }
    public string Title { set; get; }
    public string Content { set; get; }

    public virtual Blog Blog { set; get; }
}

I want to know what is the Blog's data when I remove a Post object.


Solution

  • The Entity Framework Data Provider gives you options to create your audit tables. So you must create an Audit table base on your plan and save related and extra data that you need.

    enter image description here