EntityFramework.Plus: One or more validation errors were detected during model generation

c# entity-framework entity-framework-6 entity-framework-plus

Question

I am using the audit feature of EntityFramework.Plus in an EF6 Code First project. When I add the following code:

public virtual DbSet<AuditEntry> AuditEntries { get; set; }

public virtual DbSet<AuditEntryProperty> AuditEntryProperties { get; set; }

static EntityContext()
   {
            AuditManager.DefaultConfiguration.AutoSavePreAction = (context, audit) =>
            {
                var Entities = context as EntityContext;
                if (Entities != null)
                {
                    Entities.AuditEntries.AddRange(audit.Entries);
                }
                else throw new InvalidOperationException($"Context is null for {context.Database.Connection}");
            };
        }

I get the following error:

One or more validation errors were detected during model generation:\r\n\r\nWebsite.Core.Entities.Audit: : EntityType 'Audit' has no key defined. Define the key for this EntityType.\r\nAudits: EntityType: EntitySet 'Audits' is based on type 'Audit' that has no keys defined.\r\n", "exceptionType": "System.Data.Entity.ModelConfiguration.ModelValidationException"

Audit is not an entity in my project - as stated by the error. This error is removed when I remove the code above.

Can anyone help? Is it possible to suppress model validation for external library types?

Thanks in advance

HS

Expert Answer

Disclaimer: I'm the owner of Entity Framework Plus

Looking at the error:

EntityType 'Audit' has no key defined

It seems you also mapped the Audit entity which should not be mapped.

Look at your EntityContext if you have a line similar to this and remove it:

public virtual DbSet<Audit> Audits { get; set; }

Answer Sub-Questions

We have the Audit class in our library but to have it appear in the context, it should be mapped (which we don't want). It's pretty hard to debug this issue since you are the only one to get it.

Can you try the following code in a new project and the current one? In both cases, it should work without problems.

If you can, try to provide more information to let us reproduce this issue.

using System;
using System.Data.Entity;
using System.Windows.Forms;

namespace Z.EntityFramework.Plus.Lab
{
    public partial class Form_Issue_Audit_HeySatan : Form
    {
        public Form_Issue_Audit_HeySatan()
        {
            InitializeComponent();

            using (var ctx = new EntityContext())
            {
                var audit = new Audit();

                ctx.EntitySimples.Add(new EntitySimple {ColumnInt = 1});

                ctx.SaveChanges(audit);
            }
        }

        public class EntityContext : DbContext
        {
            static EntityContext()
            {
                AuditManager.DefaultConfiguration.AutoSavePreAction = (context, audit) =>
                {
                    var Entities = context as EntityContext;
                    if (Entities != null)
                    {
                        Entities.AuditEntries.AddRange(audit.Entries);
                    }
                    else throw new InvalidOperationException($"Context is null for {context.Database.Connection}");
                };
            }

            public EntityContext() : base("CodeFirstEntities")
            {
            }

            public DbSet<EntitySimple> EntitySimples { get; set; }
            public virtual DbSet<AuditEntry> AuditEntries { get; set; }
            public virtual DbSet<AuditEntryProperty> AuditEntryProperties { get; set; }

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Types().Configure(x => x.ToTable(GetType().DeclaringType != null ? GetType().DeclaringType.FullName.Replace(".", "_") + "_" + x.ClrType.Name : ""));

                base.OnModelCreating(modelBuilder);
            }
        }

        public class EntitySimple
        {
            public int Id { get; set; }
            public int? ColumnInt { get; set; }
        }
    }
}



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why