2

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.

Masoud Darvishian
  • 3,754
  • 4
  • 33
  • 41

2 Answers2

2

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

Ali Andalibi
  • 51
  • 1
  • 3
2

If the Blog is included on the Post instance being deleted, you should get that information on the audit event.

For example if you delete like this:

var post = dbContext.Posts
    .Include(p => p.Blog)
    .First(p => p.Id == 1);
dbContext.Posts.Remove(post);
dbContext.SaveChanges();

And you are including the entity objects on the Audit.EF config:

Audit.EntityFramework.Configuration.Setup()
    .ForAnyContext(_ => _
        .IncludeEntityObjects()
    );

You should be able to get the blog information on the AuditEntityAction / CustomAction:

Audit.Core.Configuration.Setup()
    .UseEntityFramework(x => x
        .AuditTypeMapper(typeName => typeof(AuditLog))
        .AuditEntityAction<AuditLog>((ev, ent, auditEntity) =>
        {
            if (ent.Entity is Post post)
            {
                var blog = post.Blog;
            }
            // OR, if you don't IncludeEntityObjects:
            if (ent.GetEntry().Entity is Post post)
            {

            }
            //...
        }));
thepirat000
  • 12,362
  • 4
  • 46
  • 72
  • I tried it but it doesn't work. It doesn't save relations to the `column changes`. can you prepare a simple console app for `Audit.EF`? – Masoud Darvishian Feb 17 '19 at 10:06
  • The relations to external tables will **not** be on the columns changes, unless you actually map the relation column (i.e. the `BlogId`) and that column changes. That's why I've suggested to use the EF Event `.Entity` or `.GetEntry().Entity` – thepirat000 Feb 17 '19 at 19:46