簡體   English   中英

實體框架對同一個表的多個引用

[英]Entity Framework multiple references to same table

我在使用EF代碼優先創建數據庫時遇到問題。 我有一個實體玩家和實體炸船。

每個友誼都會引用兩個玩家 其中一個是發件人,另一個是友誼的接收者。

這是我的實體:

Player.cs

public class Player
{
    public int PlayerId { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public string Email { get; set; }

    [InverseProperty("Receiver")]
    public virtual List<Friendship> FriendshipsIncoming { get; set; }

    [InverseProperty("Sender")]
    public virtual List<Friendship> FriendshipsOutgoing { get; set; }
}

Friendship.cs

public class Friendship
{
    public int FriendshipId { get; set; }

    public int SenderId { get; set; }
    public int ReceiverId { get; set; }

    [ForeignKey("Sender")]
    public Player Sender { get; set; }

    [ForeignKey("Receiver")]
    public Player Receiver { get; set; }

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

我嘗試按照本教程中顯示的方式實現關系: http//www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in-code-first.aspx

當嘗試使用“update-database”命令更新數據庫時,我收到以下錯誤消息:

類型為“Darta.WebApi.Models.Friendship”的屬性“Receiver”上的ForeignKeyAttribute無效。 在依賴類型“Darta.WebApi.Models.Friendship”上找不到外鍵名稱“Receiver”。 Name值應該是以逗號分隔的外鍵屬性名稱列表。

我也嘗試使用如下所示的fluent-api解決問題: http//csharpwavenet.blogspot.sg/2013/06/multiple-foreign-keys-with-same-table.html

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Friendship>()
            .HasRequired(b => b.Sender)
            .WithMany(a => a.FriendshipsOutgoing)
            .HasForeignKey(b=>b.SenderId);

        modelBuilder.Entity<Friendship>()
            .HasRequired(b => b.Receiver)
            .WithMany(a => a.FriendshipsIncoming)
            .HasForeignKey(b => b.ReceiverId);
    }

在這種情況下,我收到以下錯誤:

在表'Friendships'上引入FOREIGN KEY約束'FK_dbo.Friendships_dbo.Players_SenderId'可能會導致循環或多個級聯路徑。 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY約束。 無法創建約束或索引。 查看以前的錯誤。

您應該只需要DataAnnotations FluentAPI。 你不需要兩者。 如果要使用[ForeignKey][InverseProperty]屬性,則刪除FluentAPI代碼。

另請注意,在[ForeignKey][InverseProperty]屬性中,您需要指定列的名稱,而不是導航屬性。

public class Player
{
    public int PlayerId { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public string Email { get; set; }

    [InverseProperty("ReceiverId")]
    public virtual ICollection<Friendship> FriendshipsIncoming { get; set; }

    [InverseProperty("SenderId")]
    public virtual ICollection<Friendship> FriendshipsOutgoing { get; set; }
}

public class Friendship
{
    public int FriendshipId { get; set; }

    public int SenderId { get; set; }
    public int ReceiverId { get; set; }

    [ForeignKey("SenderId")]
    public Player Sender { get; set; }

    [ForeignKey("ReceiverId")]
    public Player Receiver { get; set; }

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

我會糾正答案。 InverseProperty必須是有效的實體類型。 所以在這種情況下Friendship.Receiver,Friendship.Sender

public class Player {public int PlayerId {get; 組; }

   [Required]
   public string Name { get; set; }

   [Required]
   public string Email { get; set; }

   [InverseProperty("Receiver")]
   public virtual ICollection<Friendship> FriendshipsIncoming { get; set; }

   [InverseProperty("Sender")]
   public virtual ICollection<Friendship> FriendshipsOutgoing { get; set; }

}

公共類友誼{public int FriendshipId {get; 組; }

   public int SenderId { get; set; }
   public int ReceiverId { get; set; }

   [ForeignKey("SenderId")]
   public Player Sender { get; set; }

   [ForeignKey("ReceiverId")]
   public Player Receiver { get; set; }

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM