当我尝试将新的“参数”对象添加到数据库中时出现此错误,我有:
- “参数”抽象类
- “parameter_manual_input_pattern”类继承“parameter”抽象类
- “parameter_store_pattern”类继承“parameter”抽象类
- “商店”类
请注意,该类仅继承与模式相关的属性。
[Table("parameter")]
public abstract class parameter
{
[Key]
public string Parameter_Id { get; set; }
public string Pattern { get; set; }
public virtual string x { get; set; }
public virtual string y { get; set; }
[ForeignKey("Store")]
public virtual string Store_Id { get; set; }
public virtual Store Store { get; set; }
}
public class parameter_manuel_input_pattern
{
public override string x { get; set; }
}
public class parameter_store_pattern
{
public override string y { get; set; }
[ForeignKey("Store")]
public override string Store_Id { get; set; }
public override Store Store { get; set; }
}
//DbContext
public class MyContext : DbContext
{
public MyContext() : base()
{ }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<parameter>()
.Map<parameter_store_pattern>(m => m.Requires("Pattern").HasValue("store"))
.Map<parameter_manual_input_pattern>(m => m.Requires("Pattern").HasValue("manual"));
}
}