猿问

与实体框架一对一关系的抽象类

我一直在尝试使用实体框架来创建这种关系。基本上我有 1 个对象,其中有一个Result. 该Result对象是抽象的,因为它必须是继承自 的 3 个类之一Result,即ApprovedRejectedModified

我正在尝试使用实体框架创建表结构。最初我想要一个 TPCT(每个具体类型的表)结构,所以不会有表,但如果我想引用 ,Result我想将链接保留在表中,所以现在我尝试只使用 TPT 结构。我发现 TPCT 更干净,但最终如果 TPT 是实现我想要的目标的唯一方法,我就同意了。ActionResult


我已经为我的模型结构尝试了以下变体:


public class Action 

{

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]

    public int Id {get; set;}


    public int Result_Id {get; set;}


    [ForeignKey("Result_Id")]

    public virtual Result Result {get; set;}


    public string Description {get; set;}

}


public abstract class Result

{

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]

    public int Id {get; set;}


    [Required]

    public int Action_Id {get; set;}


    [ForeignKey("Action_Id")]

    public virtual Action Action {get; set;}


    public string Comment {get; set;}



    public class Approved : Result

    {

        public string Thing {get; set;}

    }


    public class Rejected : Result

    {

        public string Stuff {get; set;}

    }


    public class Modified : Result

    {

        public string Whatever {get; set;}

    }

}


然后我在上下文文件中尝试了以下 2 种策略来实现 TPT:


modelBuilder.Entity<Approved>().ToTable("Approved");

modelBuilder.Entity<Rejected>().ToTable("Rejected");

modelBuilder.Entity<Modified>().ToTable("Modified");

或者对于 TCPT:


modelBuilder.Entity<Approved>().Map(m =>

{

    m.MapInheritedProperties();

    m.ToTable("Approved");

});


每次我尝试添加新的迁移时,无论我尝试什么,我都会遇到以下错误: Unable to determine the principal end of an association between the types 'Result' and 'Action'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.


我能够让它工作的一次是如果我从课堂上删除了这个引用Action:


public int Result_Id {get; set;}


[ForeignKey("Result_Id")]

public virtual Result Result {get; set;}

但我真的很想保留该引用,这样当我进入数据库获取该Action对象时,我可以立即判断是否存在Result与其关联的对象,而无需遍历所有 3 个结果表来查看是否存在关联对象。参考这一点Action(这就是为什么我认为我需要 TPT...)



繁华开满天机
浏览 103回答 1
1回答

哔哔one

经过大量的研究和反复试验,我发现了需要什么才能得到我想要的结果。它是TPCT DB结构,并且该Action对象能够保留对Result. 以下是模型类:public class Action&nbsp;{&nbsp; &nbsp; [DatabaseGenerated(DatabaseGeneratedOption.Identity)]&nbsp; &nbsp; public int Id {get; set;}&nbsp; &nbsp; public virtual Result Result {get; set;} //just virtual here, as Action is the dependent and Result is the principal-- i.e. this Result isn't required&nbsp; &nbsp; public string Description {get; set;}}public abstract class Result{&nbsp; &nbsp; //got rid of the Result_Id, since it's 1:1 the Action_Id can be the Key&nbsp; &nbsp; [Required, Key] //added "Key"&nbsp; &nbsp; public int Action_Id {get; set;}&nbsp; &nbsp; [ForeignKey("Action_Id")]&nbsp; &nbsp; public Action Action {get; set;} //removed this virtual, as Action is Required for Result, that makes Result the principal&nbsp; &nbsp; public string Comment {get; set;}&nbsp; &nbsp; public class Approved : Result&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string Thing {get; set;}&nbsp; &nbsp; }&nbsp; &nbsp; public class Rejected : Result&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string Stuff {get; set;}&nbsp; &nbsp; }&nbsp; &nbsp; public class Modified : Result&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string Whatever {get; set;}&nbsp; &nbsp; }}以下是上下文中的流畅 API 代码://this gave me TPCT like I wantedmodelBuilder.Entity<Approved>().Map(m =>{&nbsp; &nbsp; m.MapInheritedProperties();&nbsp; &nbsp; m.ToTable("Approved");});modelBuilder.Entity<Rejected>().Map(m =>{&nbsp; &nbsp; m.MapInheritedProperties();&nbsp; &nbsp; m.ToTable("Rejected");});modelBuilder.Entity<Modified>().Map(m =>{&nbsp; &nbsp; m.MapInheritedProperties();&nbsp; &nbsp; m.ToTable("Modified");});//this defined the principal-dependent relationship I was missingmodelBuilder.Entity<Action>()&nbsp; &nbsp; .HasOptional(a => a.Result)&nbsp; &nbsp; .WithRequired(a => a.Action)&nbsp; &nbsp; .Map(x => x.MapKey("Action_Id"));然后就成功了!希望这个例子可以帮助其他人。
随时随地看视频慕课网APP
我要回答