第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

與實(shí)體框架一對(duì)一關(guān)系的抽象類(lèi)

與實(shí)體框架一對(duì)一關(guān)系的抽象類(lèi)

C#
繁華開(kāi)滿天機(jī) 2023-08-13 09:41:33
我一直在嘗試使用實(shí)體框架來(lái)創(chuàng)建這種關(guān)系。基本上我有 1 個(gè)對(duì)象,其中有一個(gè)Result. 該Result對(duì)象是抽象的,因?yàn)樗仨毷抢^承自 的 3 個(gè)類(lèi)之一Result,即Approved、Rejected或Modified:我正在嘗試使用實(shí)體框架創(chuàng)建表結(jié)構(gòu)。最初我想要一個(gè) TPCT(每個(gè)具體類(lèi)型的表)結(jié)構(gòu),所以不會(huì)有表,但如果我想引用 ,Result我想將鏈接保留在表中,所以現(xiàn)在我嘗試只使用 TPT 結(jié)構(gòu)。我發(fā)現(xiàn) TPCT 更干凈,但最終如果 TPT 是實(shí)現(xiàn)我想要的目標(biāo)的唯一方法,我就同意了。ActionResult我已經(jīng)為我的模型結(jié)構(gòu)嘗試了以下變體: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 種策略來(lái)實(shí)現(xiàn) TPT:modelBuilder.Entity<Approved>().ToTable("Approved");modelBuilder.Entity<Rejected>().ToTable("Rejected");modelBuilder.Entity<Modified>().ToTable("Modified");或者對(duì)于 TCPT:modelBuilder.Entity<Approved>().Map(m =>{    m.MapInheritedProperties();    m.ToTable("Approved");});每次我嘗試添加新的遷移時(shí),無(wú)論我嘗試什么,我都會(huì)遇到以下錯(cuò)誤: 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.我能夠讓它工作的一次是如果我從課堂上刪除了這個(gè)引用Action:public int Result_Id {get; set;}[ForeignKey("Result_Id")]public virtual Result Result {get; set;}但我真的很想保留該引用,這樣當(dāng)我進(jìn)入數(shù)據(jù)庫(kù)獲取該Action對(duì)象時(shí),我可以立即判斷是否存在Result與其關(guān)聯(lián)的對(duì)象,而無(wú)需遍歷所有 3 個(gè)結(jié)果表來(lái)查看是否存在關(guān)聯(lián)對(duì)象。參考這一點(diǎn)Action(這就是為什么我認(rèn)為我需要 TPT...)
查看完整描述

1 回答

?
嗶嗶one

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊

經(jīng)過(guò)大量的研究和反復(fù)試驗(yàn),我發(fā)現(xiàn)了需要什么才能得到我想要的結(jié)果。它是TPCT DB結(jié)構(gòu),并且該Action對(duì)象能夠保留對(duì)Result. 以下是模型類(lèi):


public class Action 

{

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]

    public int Id {get; set;}


    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


    public string Description {get; set;}

}


public abstract class Result

{

    //got rid of the Result_Id, since it's 1:1 the Action_Id can be the Key


    [Required, Key] //added "Key"

    public int Action_Id {get; set;}


    [ForeignKey("Action_Id")]

    public Action Action {get; set;} //removed this virtual, as Action is Required for Result, that makes Result the principal


    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;}

    }

}

以下是上下文中的流暢 API 代碼:


//this gave me TPCT like I wanted

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

{

    m.MapInheritedProperties();

    m.ToTable("Approved");

});


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

{

    m.MapInheritedProperties();

    m.ToTable("Rejected");

});


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

{

    m.MapInheritedProperties();

    m.ToTable("Modified");

});


//this defined the principal-dependent relationship I was missing

modelBuilder.Entity<Action>()

    .HasOptional(a => a.Result)

    .WithRequired(a => a.Action)

    .Map(x => x.MapKey("Action_Id"));

然后就成功了!希望這個(gè)例子可以幫助其他人。


查看完整回答
反對(duì) 回復(fù) 2023-08-13
  • 1 回答
  • 0 關(guān)注
  • 145 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)