我对这个程序结构和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”关系与概念模型中定义的任何关系都不匹配。
问题是“Formula”由于GenericType继承而找不到Operand1和Operand2 Ids relationShip
你有什么建议吗?
这是classDiagram的链接: http://imageshack.us/a/img443/1854/jl98.png
非常感谢!
答案 0 :(得分:0)
检查下面提到的链接。@ John解释了上述问题如下。
EF在您尝试时不支持继承。它会工作 使用抽象类,但非虚拟属性。通用无意义 支持,但不支持来自具有虚拟属性的抽象实体。 您可以拥有一个抽象实体,但需要非虚拟属性。
了解更多信息:G+ Solution