1

I have an existing application that is built on Entity Framework Core 2.2.x. It is using modelBuilder.ApplyConfiguration() to associate entities with the data model dynamically. This works for all of the current entities and even my new AuditLog entity as far as the rest of the application is concerned.

However, when I configure Audit.NET's entity framework core provider to log into AuditLog, the data provider cannot write to the database:

The entity type 'AuditLog' was not found. Ensure that the entity type has been added to the model.

I have scoured the internet for solutions to that error, and found that adding this line to my code will cause Audit.NET to find my AuditLog:

modelBuilder.Entity<AuditLog>().ToTable("AuditLog", "Audit");

My code:

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            Type[] maps = EntityFrameworkReflectionMapping.Get(EntityTypeConfiguration(), BoundAssemblies);

            foreach (object instance in maps.Select(Activator.CreateInstance))
                modelBuilder.ApplyConfiguration((dynamic)instance);

            modelBuilder.Entity<AuditLog>().ToTable("AuditLog", "Audit");

            base.OnModelCreating(modelBuilder);
        }

Why do I need to add the entity explicitly, when the rest of the system works as-is?

Additionally, the changes are being detected by Audit.NET through entities which are not explicitly added. So the problem seems to be with Audit.NET's entity framework data provider, or how I'm using it.

I would expect that the data provider would respect the modelBuilder.ApplyConfiguration() approach to associating entities.

wildbagel
  • 145
  • 8
  • Does your `DbContext` has a `DbSet` public property? Just guessing it could be because you don't have such property for `AuditLog`, but you do for the other tables that works. – thepirat000 Jul 24 '19 at 21:12
  • Is the `AuditLog` defined on the same assembly as the other entities? Also have you seen [this](https://stackoverflow.com/a/40388052/122195) answer? – thepirat000 Jul 25 '19 at 02:46

1 Answers1

0

There are many things that could be causing the exception, but looks like EF is not being able to detect the entity-table relation for your AuditLog.

Look for a wrong connection string, maybe your AuditLog being defined on a different assembly than other entities.

Also try adding the AuditLog entity class within a db set as a property on your DbContext, for example:

public class MyContext : AuditDbContext
{
    //...
    public DbSet<ModelName> ModelName { get; set; }
}
thepirat000
  • 12,362
  • 4
  • 46
  • 72
  • AuditLog is defined in a separate project from the entity that is being audited. Is that significant? Also, when I add DbSet to the DbContext object, I then get this when it tries to log: `Invalid object name 'AuditLog'.` – wildbagel Jul 25 '19 at 16:37
  • Yes I guess that's the problem. Check [this](https://patrickdesjardins.com/blog/how-to-register-model-builder-without-having-to-manually-add-them-one-by-one) and [this](https://stackoverflow.com/a/33104651/122195) – thepirat000 Jul 25 '19 at 20:55