C# 映射子类的外键
我有以下抽象类:C# 映射子类的外键,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我有以下抽象类: public abstract class ClauseComponent { public int ClauseComponentId { get; set; } public abstract string[] Determinate(ClimateChart chart); public abstract List<ClauseComponent> GiveCorrectPath(ClimateChart
public abstract class ClauseComponent
{
public int ClauseComponentId { get; set; }
public abstract string[] Determinate(ClimateChart chart);
public abstract List<ClauseComponent> GiveCorrectPath(ClimateChart chart);
public abstract String GetHtmlCode(Boolean isYes);
public virtual void Add(Boolean soort, ClauseComponent component)
{
throw new ApplicationException();
}
public ClauseComponent()
{
}
}
public class Clause : ClauseComponent
{
public virtual ClauseComponent YesClause { get; set; }
public virtual ClauseComponent NoClause { get; set; }
public String Name { get; private set; }
public virtual Parameter Par1 { get; set; }
public virtual Parameter Par2 { get; set; }
public int Waarde { get; set; }
public String Operator { get; set; }
public Clause()
{
}
public Clause(String name, Parameter par1, String op, int waarde)
{
this.Name = name;
this.Par1 = par1;
this.Operator = op;
this.Waarde = waarde;
}
public Clause(String name, Parameter par1, Parameter par2)
{
this.Name = name;
this.Par1 = par1;
this.Par2 = par2;
}
}
这是抽象类的映射器(我没有子类的映射器):
我的数据库中有:
现在我想给映射取一个合适的名称,我怎样才能做到这一点呢?
我从来没有在抽象类和子类上做过映射,所以我在这里有点不知所措。一种方法是为映射列创建属性,在映射类中,使用映射列属性映射虚拟属性 例如 或Fluent Api:
modelBuilder.Entity<Clause >().HasRequired(p => p.Par1 ) // Or Optional
.HasForeignKey(p => p.MyCustomPar1Id);
modelBuilder.Entity().HasRequired(p=>p.Par1)//或可选
.HasForeignKey(p=>p.MyCustomPar1Id);
让我知道它是否有效,我还没有测试过它,但如果我的记忆正常,它应该可以工作。我尝试过它,但没有工作。。。我应该将fluent api的代码放在哪里?用什么制图器?因为我只有一个抽象类的映射器。
public class Clause : ClauseComponent
{
public int MyCustomPar1Id{ get; set; }
[ForeignKey("MyCustomPar1Id")]
public virtual Parameter Par1 { get; set; }
}
modelBuilder.Entity<Clause >().HasRequired(p => p.Par1 ) // Or Optional
.HasForeignKey(p => p.MyCustomPar1Id);