关于c#:EntityFramework.Plus:在模型生成过程中检测到一个或多个验证错误

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

我在EF6 Code First项目中使用EntityFramework.Plus的审核功能。 当我添加以下代码时:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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}");
            };
        }

我收到以下错误:

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

正如错误所指出的,审计不是我项目中的实体。 当我删除上面的代码时,将删除此错误。

有人可以帮忙吗? 是否可以禁止对外部库类型进行模型验证?

提前致谢

HS


免责声明:我是Entity Framework Plus的所有者

查看错误:

EntityType 'Audit' has no key defined

似乎您还映射了不应映射的审核实体。

查看您的EntityContext,如果有与此相似的行并将其删除:

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

回答子问题

我们的库中有Audit类,但是要使其出现在上下文中,应该对其进行映射(我们不希望如此)。 调试此问题非常困难,因为您是唯一获得此问题的人。

您可以在新项目和当前项目中尝试以下代码吗? 在这两种情况下,它都可以正常工作。

如果可以,请尝试提供更多信息,以使我们重现此问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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; }
        }
    }
}