2

我对这个程序结构和 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

非常感谢 !

4

1 回答 1

0

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

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

欲了解更多信息:G+ 解决方案

And :使用实体框架实现继承

于 2013-11-22T20:40:03.913 回答