[英]What is default delete option in EF Core when a table have more than one foreign key?
[英]What is the foreign key if more than one pair of navigation properties exists?
以下內容摘自官方文檔。
您可以使用數據注釋來配置從屬實體和主體實體上的導航屬性如何配對。 這通常在兩種實體類型之間存在一對以上導航屬性時完成。
public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public int AuthorUserId { get; set; } public User Author { get; set; } public int ContributorUserId { get; set; } public User Contributor { get; set; } } public class User { public string UserId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } [InverseProperty("Author")] public List<Post> AuthoredPosts { get; set; } [InverseProperty("Contributor")] public List<Post> ContributedToPosts { get; set; } }
題
由於我是 EF Core 的新手,如果存在一對以上的導航屬性,那么外鍵是什么?
你的代碼會以這種方式工作,
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
[ForeignKey("Author")]
public int AuthorUserId { get; set; }
public virtual User Author { get; set; }
[ForeignKey("Contributor")]
public int ContributorUserId { get; set; }
public virtual User Contributor { get; set; }
}
public class User
{
public string UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
[InverseProperty("Author")]
public ICollection<Post> AuthoredPosts { get; set; }
[InverseProperty("Contributor")]
public ICollection<Post> ContributedToPosts { get; set; }
}
干杯,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.