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

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

Entity Framework Core:Fluent api 多對(duì)多

Entity Framework Core:Fluent api 多對(duì)多

C#
ibeautiful 2021-07-07 16:55:09
我如何建模以下內(nèi)容:一個(gè)用戶有很多關(guān)注者并關(guān)注很多用戶。同一個(gè)用戶有許多被阻止的用戶(Twitter 有點(diǎn)功能)。public class ApplicationUser : IdentityUser{    public virtual ICollection<User> Following { get; set; }    public virtual ICollection<User> Followers { get; set; }    public virtual ICollection<User> BlockedUsers { get; set; }}public class User{    public ApplicationUser User { get; set; }    public string UserId { get; set; }    public ApplicationUser Follower { get; set; }    public string FollowerId { get; set; }}到目前為止我的實(shí)現(xiàn):public void Configure(EntityTypeBuilder<User> builder){    builder.HasKey(k => new { k.UserId, k.FollowerId });    builder.HasOne(l => l.User)           .WithMany(a => a.Followers)           .HasForeignKey(l => l.UserId);    builder.HasOne(l => l.Follower)           .WithMany(a => a.Following)           .HasForeignKey(l => l.FollowerId);}如何實(shí)現(xiàn)被阻止的用戶字段?public virtual ICollection<User> BlockedUsers { get; set; }
查看完整描述

1 回答

?
偶然的你

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

正如聊天中所討論的,我很少相信 EF 的多對(duì)多功能,更不用說(shuō) EF Core。這不是直接針對(duì)您的問(wèn)題,而是解釋了如果這是我的項(xiàng)目我將如何處理。


您已經(jīng)擁有ApplicationUser了,因此從它們中分離出來(lái)的表只會(huì)存在于定義不同ApplicationUsers. 每個(gè)用戶可以擁有多個(gè)所有內(nèi)容:關(guān)注者、關(guān)注者和阻止。用戶并不直接控制誰(shuí)跟隨他們,所以不需要自己的表。您可以通過(guò)查看關(guān)注者表來(lái)確定誰(shuí)關(guān)注了用戶。


public class ApplicationUser : IdentityUser

{

    public virtual ICollection<UserFollow> Following { get; set; }

    public virtual ICollection<UserFollow> Followers { get; set; }

    public virtual ICollection<UserBlock> BlockedUsers { get; set; }

}


public class UserFollow

{

    public int Id { get; set; }


    [ForeignKey(nameof(SourceUserId))]

    public ApplicationUser SourceUser { get; set; }

    public string SourceUserId { get; set; }


    [ForeignKey(nameof(FollowedUserId))]

    public ApplicationUser FollowedUser { get; set; }

    public string FollowedUserId { get; set; }

}


public class UserBlock

{

    public int Id { get; set; }


    [ForeignKey(nameof(SourceUserId))]

    public ApplicationUser SourceUser { get; set; }

    public string SourceUserId { get; set; }


    [ForeignKey(nameof(BlockedUserId))]

    public ApplicationUser BlockedUser { get; set; }

    public string BlockedUserId { get; set; }

}

然后你的配置不會(huì)有太大變化(考慮這個(gè)偽,未經(jīng)測(cè)試):


protected override void OnModelCreating(ModelBuilder modelBuilder)

{

    moderlBuilder.Entity<UserFollow>()

           .HasOne(l => l.SourceUser)

           .WithMany(a => a.Following)

           .HasForeignKey(l => l.SourceUserId);


    moderlBuilder.Entity<UserFollow>()

           .HasOne(l => l.FollowedUser)

           .WithMany(a => a.Followers)

           .HasForeignKey(l => l.FollowedUserId);


    moderlBuilder.Entity<UserBlock>()

           .HasOne(l => l.SourceUser)

           .WithMany(a => a.BlockedUsers)

           .HasForeignKey(l => l.SourceUserId);

}

(請(qǐng)注意,我總是使用一個(gè)簡(jiǎn)單的鍵(Id只是為了便于查詢),但您可以根據(jù)需要將其改回復(fù)合鍵)


查看完整回答
反對(duì) 回復(fù) 2021-07-10
  • 1 回答
  • 0 關(guān)注
  • 196 瀏覽

添加回答

舉報(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)