【问题标题】:RelationShip error on EF code-firstEF 代码优先的关系错误
【发布时间】:2023-03-23 16:06:01
【问题描述】:

我对这个程序结构和 EF 6 有一个奇怪的问题。

public abstract class Operand
{
    public virtual int Id
    {
        get;
        set;
    }
}

public class Formula : Operand
{
    public Operand Operand1
    {
        get;
        set;
    }

    public Operand Operand2
    {
        get;
        set;
    }

    public String Operator
    {
        get;
        set;
    }


    public override int Id
    {
        get;
        set;
    }
}

public class OperandValue<T> : Operand
{
    public T Value
    {
        get;
        set;
    }


    public override int Id
    {
        get;
        set;
    }
}

public class OperandInt : OperandValue<int>
{
}

public class ModelEntity : DbContext
{
    public ModelEntity()
        : base("MyCnxString")
    {
        this.Configuration.LazyLoadingEnabled = true;
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Types<Formula>()
        .Configure(c => c.ToTable(c.ClrType.Name));

        modelBuilder.Types<OperandInt>()
        .Configure(c => c.ToTable(c.ClrType.Name));
    }

    public DbSet<Formula> Formula { get; set; }
    public DbSet<OperandInt> OperandValue { get; set; }

}

还有我的测试程序:

public class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Starting...");


        var migration = new TestEntity2.Migrations.Configuration();
        migration.AutomaticMigrationsEnabled = true;
        migration.AutomaticMigrationDataLossAllowed = true;

        var migrator = new System.Data.Entity.Migrations.DbMigrator(migration);
        migrator.Update();


        using (ModelEntity db = new ModelEntity())
        {
            OperandInt opValue1 = new OperandInt() { Value = 3 };
            db.OperandValue.Add(opValue1);

            OperandInt opValue2 = new OperandInt() { Value = 4 };
            db.OperandValue.Add(opValue2);

            Formula formula = new Formula() { Operand1 = opValue1, Operand2 = opValue2, Operator = "*" };
            db.Formula.Add(formula);


            db.SaveChanges();

            Console.WriteLine("Ended !");
            Console.ReadLine();
        }
    }
}

当我执行这个程序时,我得到这个错误信息:

关系“Formula_Operand1”与概念模型中定义的任何关系都不匹配。

问题是“公式”由于 GenericType 继承而找不到 Operand1 和 Operand2 Ids 关系

你有什么建议吗?

这是一个指向 classDiagram 的链接: http://imageshack.us/a/img443/1854/jl98.png

非常感谢!

【问题讨论】:

  • 请添加语言标签。

标签: c# entity-framework generics inheritance


【解决方案1】:

检查下面提到的链接。@John 对上述问题的解释如下。

EF 不支持您尝试的继承。它会工作 具有抽象类,但具有非虚拟属性。通用继承 支持,但不支持具有虚拟属性的抽象实体。 您可以拥有一个抽象实体,但需要非虚拟属性。

欲了解更多信息:G+ Solution

还有:Implementing Inheritance with the Entity Framework

【讨论】:

    猜你喜欢
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 2013-05-02
    • 1970-01-01
    相关资源
    最近更新 更多