【问题标题】:EntityFramework composite foreign key with inheratince具有继承的实体框架复合外键
【发布时间】:2020-07-11 06:53:59
【问题描述】:

我有一个实体框架项目,它有这样的模型和配置。

public class Context : DbContext
{
    public DbSet<Field> Fields { get; set; }

    public DbSet<Table> Tables { get; set; }

    public DbSet<Column> Columns { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Configurations.Add(new FieldConfiguration());
        modelBuilder.Configurations.Add(new TableConfiguration());
        modelBuilder.Configurations.Add(new ColumnConfiguration());
    }
}

public class Field
{
    public Guid Guid { get; set; }

    public Guid CompanyGuid { get; set; }
}

public class Column : Field
{
    public Guid TableGuid { get; set; }

    public Table Table { get; set; }
}

public class Table : Field
{
    public List<Column> Columns { get; set; }
}

public class FieldConfiguration : EntityTypeConfiguration<Field>
{
    public FieldConfiguration()
    {
        Map(t =>
        {
            t.ToTable("Field");
        });
        HasKey(p => new { p.Guid, p.CompanyGuid });
        Property(p => p.Guid).HasColumnName("Guid").IsRequired();
        Property(p => p.CompanyGuid).HasColumnName("CompanyGuid").IsRequired();

    }
}

public class ColumnConfiguration : EntityTypeConfiguration<Column>
{
    public ColumnConfiguration()
    {
        Map(t =>
        {
            t.ToTable("Column");
        });
        HasKey(p => new { p.Guid, p.CompanyGuid });
        Property(p => p.Guid).HasColumnName("Guid").IsRequired();
        Property(p => p.CompanyGuid).HasColumnName("CompanyGuid").IsRequired();
    }
}

public class TableConfiguration : EntityTypeConfiguration<Table>
{
    public TableConfiguration()
    {
        Map(t =>
        {
            t.ToTable("Table");
        });
        HasKey(p => new { p.Guid, p.CompanyGuid });
        Property(p => p.Guid).HasColumnName("Guid").IsRequired();
        Property(p => p.CompanyGuid).HasColumnName("CompanyGuid").IsRequired();
        HasMany(t => t.Columns).WithRequired(t => t.Table).HasForeignKey(t => new { t.TableGuid, t.CompanyGuid }).WillCascadeOnDelete(true);
    }
}

然后我尝试创建一个迁移,我得到这样的错误

外键组件“CompanyGuid”不是“列”类型的声明属性。验证它没有被明确地从模型中排除,并且它是一个有效的原始属性。

怎么了?我该如何解决?

【问题讨论】:

    标签: c# .net entity-framework


    【解决方案1】:

    这里问题的根源是Field 实体。它用作数据库表,同时用作其他表实体的基类。 EF 不允许用于外键约束的属性位于具有继承性的多个表上,例如在这种情况下,CompanyGuid 属性是Field 及其派生Column 实体的列。

    为了克服这个 EF 限制,我可以为您提出两个解决方案:

    1. 第一个选项,创建一个完全独立的基类并从该基类派生所有 3 个实体:
        public abstract class EntityBase
        {
            public Guid Guid { get; set; }
    
            public Guid CompanyGuid { get; set; }
        }
    
        public class Field : EntityBase
        {
        }
    
        public class Column : EntityBase
        {
            public Guid TableGuid { get; set; }
    
            public Table Table { get; set; }
        }
    
        public class Table : EntityBase
        {
            public List<Column> Columns { get; set; }
        }
    
    1. 第二个选项,删除Field继承并将其字段显式添加到TableColumn实体,如下所示:
        public class Field
        {
            public Guid Guid { get; set; }
    
            public Guid CompanyGuid { get; set; }
        }
    
        public class Column
        {
            public Guid Guid { get; set; }
    
            public Guid CompanyGuid { get; set; }
    
            public Guid TableGuid { get; set; }
    
            public Table Table { get; set; }
        }
    
        public class Table
        {
            public Guid Guid { get; set; }
    
            public Guid CompanyGuid { get; set; }
    
            public List<Column> Columns { get; set; }
        }
    

    【讨论】:

    • 谢谢!现在我明白了一种情况。
    猜你喜欢
    • 2015-08-25
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多