The entity type AuditLog is not part of the model for the current context.
See original GitHub issueIssue
I intermittently get the following error:
System.InvalidOperationException: The entity type AuditLog is not part of the model for the current context.
I have difficulty reproducing the error and when I do, there doesn’t seem to be any kind of pattern.
Code
DatabaseContext.cs
public class DatabaseContext : AuditDbContext
{
public DatabaseContext(string contextName) : base(contextName)
{
Configuration.LazyLoadingEnabled = false;
Configuration.ProxyCreationEnabled = false;
ConfigureAuditNet();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<AuditLog>().ToTable("AuditLogs");
base.OnModelCreating(modelBuilder);
}
private void ConfigureAuditNet()
{
Audit.Core.Configuration.Setup()
.UseEntityFramework(_ => _
.AuditTypeMapper(t => typeof(AuditLog))
.AuditEntityAction<AuditLog>((ev, entry, entity) =>
{
entity.AuditData = entry.ToJson();
entity.EntityType = entry.EntityType.Name;
entity.AuditDate = DateTime.Now;
entity.AuditUserId = HttpContext.Current.User.GetUserIdInt();
entity.TablePk = entry.PrimaryKey.First().Value.ToString();
})
.IgnoreMatchedProperties(true));
Audit.EntityFramework.Configuration.Setup()
.ForContext<DatabaseContext>()
.UseOptOut()
.Ignore<Heartbeat>();
}
public DatabaseContext() : base("name=OPv2Database") { }
public DbSet<AuditLog> AuditLogs { get; set; }
.
.
.
// some more models
}
How I save to the database.
public void AddShopOrderNotes(List<ShopOrderNote> shopOrderNotes)
{
using (var context = new DatabaseContext(_connectionName))
{
context.ShopOrderNotes.AddRange(shopOrderNotes);
context.SaveChanges();
}
}
This will work anywhere from 80% - 95% of the time. The other times it will fail, then the user will save again immediately and it will work.
Other Information
packages.config
in my project that defines the DatabaseContext
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Audit.EntityFramework" version="15.1.1" targetFramework="net461" />
<package id="Audit.NET" version="15.1.1" targetFramework="net461" />
<package id="EntityFramework" version="6.2.0" targetFramework="net461" />
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net461" />
</packages>
packages.config
in my web api project
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Antlr" version="3.5.0.2" targetFramework="net461" />
<package id="Audit.EntityFramework" version="15.1.1" targetFramework="net461" />
<package id="Audit.NET" version="15.1.1" targetFramework="net461" />
<package id="AutoMapper" version="9.0.0" targetFramework="net461" />
<package id="bootstrap" version="3.3.7" targetFramework="net461" />
<package id="BouncyCastle" version="1.8.5" targetFramework="net461" />
<package id="Destructurama.Attributed" version="2.0.0" targetFramework="net461" />
<package id="Destructurama.ByIgnoring" version="1.0.4" targetFramework="net461" />
<package id="EntityFramework" version="6.2.0" targetFramework="net461" />
<package id="jQuery" version="3.3.1" targetFramework="net461" />
<package id="MailKit" version="2.4.1" targetFramework="net461" />
<package id="Microsoft.AspNet.Cors" version="5.2.7" targetFramework="net461" />
<package id="Microsoft.AspNet.Mvc" version="5.2.4" targetFramework="net461" />
<package id="Microsoft.AspNet.Razor" version="3.2.4" targetFramework="net461" />
<package id="Microsoft.AspNet.TelemetryCorrelation" version="1.0.0" targetFramework="net461" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi" version="5.2.4" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.7" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.7" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.Cors" version="5.2.7" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.HelpPage" version="5.2.4" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.4" targetFramework="net461" />
<package id="Microsoft.AspNet.WebPages" version="3.2.4" targetFramework="net461" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="1.1.0" targetFramework="net461" />
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="2.0.0" targetFramework="net461" />
<package id="Microsoft.Extensions.Configuration" version="3.1.0" targetFramework="net461" />
<package id="Microsoft.Extensions.Configuration.Abstractions" version="3.1.0" targetFramework="net461" />
<package id="Microsoft.Extensions.Configuration.Binder" version="3.1.0" targetFramework="net461" />
<package id="Microsoft.Extensions.DependencyInjection" version="3.1.0" targetFramework="net461" />
<package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="3.1.0" targetFramework="net461" />
<package id="Microsoft.Extensions.Logging.Abstractions" version="3.1.0" targetFramework="net461" />
<package id="Microsoft.Extensions.Options" version="3.1.0" targetFramework="net461" />
<package id="Microsoft.Extensions.Primitives" version="3.1.0" targetFramework="net461" />
<package id="Microsoft.IdentityModel.JsonWebTokens" version="5.4.0" targetFramework="net461" />
<package id="Microsoft.IdentityModel.Logging" version="5.4.0" targetFramework="net461" />
<package id="Microsoft.IdentityModel.Tokens" version="5.4.0" targetFramework="net461" />
<package id="Microsoft.VisualStudio.SlowCheetah" version="3.2.26" targetFramework="net461" developmentDependency="true" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net461" />
<package id="MimeKit" version="2.4.1" targetFramework="net461" />
<package id="Modernizr" version="2.8.3" targetFramework="net461" />
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net461" />
<package id="Serilog" version="2.9.0" targetFramework="net461" />
<package id="Serilog.Settings.AppSettings" version="2.2.2" targetFramework="net461" />
<package id="Serilog.Sinks.File" version="4.1.0" targetFramework="net461" />
<package id="SerilogWeb.Classic" version="5.0.48" targetFramework="net461" />
<package id="SerilogWeb.Classic.WebApi" version="4.0.5" targetFramework="net461" />
<package id="System.Buffers" version="4.4.0" targetFramework="net461" />
<package id="System.ComponentModel.Annotations" version="4.7.0" targetFramework="net461" />
<package id="System.Diagnostics.DiagnosticSource" version="4.4.1" targetFramework="net461" />
<package id="System.IdentityModel.Tokens.Jwt" version="5.4.0" targetFramework="net461" />
<package id="System.Memory" version="4.5.2" targetFramework="net461" />
<package id="System.Numerics.Vectors" version="4.4.0" targetFramework="net461" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.7.0" targetFramework="net461" />
<package id="System.Threading.Tasks.Extensions" version="4.5.2" targetFramework="net461" />
<package id="WebGrease" version="1.6.0" targetFramework="net461" />
</packages>
Please let me know if you need any other information. I’m stumped on what it could be and I’m willing to try any suggestions.
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (6 by maintainers)
Top Results From Across the Web
The entity type <type> is not part of the model for ...
This solved my issue. For me the issue was that I had not included the Entity Class within my db set inside the...
Read more >error The entity type AuditLog is not part of the model for ...
Hello sir, big fan of your work but I ran in to this error, I build a project using asp.net with Entity Framework,...
Read more >The entity type PopularToys is not part of the model for ...
Core and Nop.Data). Now when I try to fetch this table I get following error: The entity type PopularToys is not part of...
Read more >Entity type is not part of the model for the current context….
1). Check that your properties are nullable. In the Edmx select each field and then click properties(should be next to solution explorer in...
Read more >[c#] The entity type <type> is not part of the model for ...
System.InvalidOperationException : The entity type Estate is not part of the model for the current context. It happens when I am trying to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That’s the way to go. The data provider should be set just once, since it’s a global static property (
Audit.Core.Configiration.DataProvider
)And thank you very much for your kind words 😃
Now it saves and I don’t get any errors after doing this: