我对这个程序结构和 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
非常感谢 !