اگر شما در یک جدول دوتا کلید خارجی دارید بهتره از Attribute Inverse Property بهره ببیرد
به دلیل اینکه فیلدهای اضافی و با اسامی نا مفهوم در چدول بوجود نیاید
public class User
{
public int UserId { get; set; }
public string UserName { get; set; }
[ForeignKey("RoleId ")]
public Role Role{ get; set; }
public int RoleId { get; set; }
[ForeignKey("RoleId1 ")]
public Role Role1{ get; set; }
public int RoleId1 { get; set; }
}
public class Role
{
public int RoleId { get; set; }
public string Name { get; set; }
[InverseProperty("Role")]
public ICollection<User> Users1{ get; set; }
[InverseProperty("Role1")]
public ICollection<User> Users2{ get; set; }
}
دقت کنید در کلاس Role عنوان InverseProperty تنظیم شده به Role وRole1 و باید در کلاس User هم نام Navigation Property هاتون همین نام داشته باشد
مثل:
public Role Role{ get; set; }public Role Role1{ get; set; }
نیازی به کلاسهای Config و Fluent Api هم ندارید با همین DataAnnotations Attributes میتونید به راحتی روابط را برقرار کنید