1

我正在用Friend函数构建一个社交网络。 我的想法是,我已经有默认ApplicationUser类,所以我创建了一个新的表名为朋友实体框架代码优先,同一个表上的多对多关系

public class Friend 
{ 
    [Key] 
    [Column(Order = 1)] 
    public string SenderId { get; set; } 

    [Key] 
    [Column(Order = 2)] 
    public string ReceiverId { get; set; } 

    //Status == true : Friend request accepted 
    //Status == false : Friend request not accepted 
    public bool Status { get; set; } 
} 

ApplicationUser,我定义2个导航性能发件人接收机 (链接到朋友表)

public class ApplicationUser : IdentityUser 
{ 
    [Required] 
    [StringLength(50)] 
    public string Name { get; set; } 

    [Required] 
    public bool Gender { get; set; } 

    [StringLength(255)] 
    public string Address { get; set; } 

    [StringLength(255)] 
    public string Job { get; set; } 

    [StringLength(255)] 
    public string Image { get; set; } 

    public DateTime Birthday { get; set; } 

    public ICollection<ApplicationUser> Senders { get; set; } 
    public ICollection<ApplicationUser> Receivers { get; set; } 
} 

最后在ApplicationDbContext中,我使用Flue声明了2个表之间的关系NT阿比

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 

    public DbSet<Friend> Friends { get; set; } 

    public ApplicationDbContext() 
     : base("DefaultConnection", throwIfV1Schema: false) 
    { 
    } 

    public static ApplicationDbContext Create() 
    { 
     return new ApplicationDbContext(); 
    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<ApplicationUser>() 
      .HasMany(a => a.Senders) 
      .WithMany(a => a.Receivers) 
      .Map(m => 
      { 
       m.MapLeftKey("ReceiverId"); 
       m.MapRightKey("SenderId"); 
       m.ToTable("Friends"); 
      }); 
     base.OnModelCreating(modelBuilder); 

    } 
} 

但是当我添加迁移,它会创建2个表像这样,和他们都不是我需要的东西(一个没有外键,一个没有状态属性)

public override void Up() 
    { 
     CreateTable(
      "dbo.Friends1", 
      c => new 
       { 
        SenderId = c.String(nullable: false, maxLength: 128), 
        ReceiverId = c.String(nullable: false, maxLength: 128), 
        Status = c.Boolean(nullable: false), 
       }) 
      .PrimaryKey(t => new { t.SenderId, t.ReceiverId }); 

     CreateTable(
      "dbo.Friends", 
      c => new 
       { 
        SenderId = c.String(nullable: false, maxLength: 128), 
        ReceiverId = c.String(nullable: false, maxLength: 128), 
       }) 
      .PrimaryKey(t => new { t.SenderId, t.ReceiverId }) 
      .ForeignKey("dbo.AspNetUsers", t => t.SenderId) 
      .ForeignKey("dbo.AspNetUsers", t => t.ReceiverId) 
      .Index(t => t.SenderId) 
      .Index(t => t.ReceiverId); 

    } 

那我该怎么做:(我搜索互联网上,这似乎合法但它不起作用

回答

1

但是当我添加,移动,它会创建2个表像这样,和他们都不是我需要的(一个没有外键,一个没有'吨有状态的属性)

这是因为混合由EF支持的两种可能many-to-many协会 - 一个使用隐式接线表(从流利API配置),另一个使用明确的结表(Friend实体)和两个one-to-many协会。

由于只有在没有与关联关联的附加数据且您有一个(Status属性)时才能使用第一种方法,所以您需要使用第二种方法。

为了做到这一点,如下更改导航属性的类型:

public class ApplicationUser : IdentityUser 
{ 
    // ... 
    public ICollection<Friend> Senders { get; set; } 
    public ICollection<Friend> Receivers { get; set; } 
} 

和配置:

modelBuilder.Entity<ApplicationUser>() 
    .HasMany(e => e.Senders) 
    .WithRequired() 
    .HasForeignKey(e => e.ReceiverId); 

modelBuilder.Entity<ApplicationUser>() 
    .HasMany(e => e.Receivers) 
    .WithRequired() 
    .HasForeignKey(e => e.SenderId); 
+0

你刚才救了我的一天,谢谢! – LaXuanLinh

+0

还有一个问题,我如何检索一个用户的朋友列表? – LaXuanLinh

+0

我需要一个ApplicationUser的列表,它们是另一个ApplicationUser的朋友。所以我通过它的ID和查询如下 – LaXuanLinh

相关问题