0

指定類屬性 'DepartmentID的' 不能被配置爲導航屬性

public class Designation 
{ 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    [Key] 
    public int id { get; set; } 

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    [ForeignKey("DepartmentID")] 
    public int DepartmentID { get; set; } 



    public bool IsActive { get; set; } 

    public string AddedBy { get; set; } 

    public DateTime AddedDate { get; set; } 
    public string UpdateBy { get; set; } 

    public DateTime UpdateDate { get; set; } 
    public virtual Department Department { get; set; } 
} 

系類

public class Department 
{ 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    [Key] 
    public int DepartmentID { get; set; } 
    public string DepartmentName { get; set; } 

    public bool IsActive { get; set; } 

    public string AddedBy { get; set; } 

    public DateTime AddedDate { get; set; } 
    public string UpdateBy { get; set; } 

    public DateTime UpdateDate { get; set; } 
    public virtual ICollection<Designation> Designations { get; set; } 
} 

EmployeeInfo類

public class EmployeeInfo 
{ 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    [Key] 
    public int EmployeeID { get; set; } 
    public int DepartmentID { get; set; } 
    [ForeignKey("DepartmentID")] 
    public virtual Department Department { get; set; } 
    [Required(ErrorMessage = "Employee Name is Required"), Display(Name = "Name")] 
    public string EmployeeName { get; set; } 
    [Required(ErrorMessage = "Employee Bangla Name is Required"), Display(Name = "Bangla Name")] 
    public string EmployeeBanglaName { get; set; } 
    [Required(ErrorMessage = "Department Name is Required")] 
    public string Department { get; set; } 
    [Required(ErrorMessage = "Designation is Required")] 
    public string Designation { get; set; } 
    [DataType(DataType.Date)] 
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] 
    public DateTime JoiningDate{ get; set; } 

    [DataType(DataType.Date)] 
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] 
    public DateTime DateOfBirth { get; set; } 
    [Required(ErrorMessage = "BloodGroup is Required"), Display(Name = "Blood Group")] 
    public string BloodGroup { get; set; } 
    [Required(ErrorMessage = "Gender is Required")]  
    public gender Gender { get; set; } 
    [Required(ErrorMessage = "Father Name is Required"), Display(Name = "Father Name")] 
    public string FatherName { get; set; } 
    [Required(ErrorMessage = "Mother Name is Required"), Display(Name = "Mother Name")] 
    public string MotherName { get; set; } 
    [Required(ErrorMessage = "Husband/Wife Name is Required"), Display(Name = "Spouse Name")] 
    public string SpouseName { get; set; } 
    public string ChildrenNumber { get; set; } 
    public string CardNumber { get; set; } 
    [Required(ErrorMessage = "Contact No is Required"), DataType(DataType.PhoneNumber)] 
    public string ContactNo { get; set; } 
    [Required(ErrorMessage = "Email is Required"), DataType(DataType.EmailAddress), StringLength(200)] 
    public string Email { get; set; } 

    [Required(ErrorMessage = "HomeContactNo is Required"),DataType(DataType.PhoneNumber)] 
    public string HomeContactNo { get; set; } 
    [Required(ErrorMessage = "District Name is Required")] 
    public string District { get; set; } 
    [Required(ErrorMessage = "Thana is Required")] 
    public string Thana { get; set; } 
    [Required(ErrorMessage = "Village Name is Required")] 
    public string Vill { get; set; } 
    [Required(ErrorMessage = "Post Code is Required")] 
    public string PostCode { get; set; } 
    public string Image { get; set; } 
    public byte[] picture { get; set; } 
    public bool IsActive { get; set; } 

    public string AddedBy { get; set; } 

    public DateTime AddedDate { get; set; } 
    public string UpdateBy { get; set; } 

    public DateTime UpdateDate { get; set; } 
} 

性別枚舉

public enum gender 
{ 
    Male, 
    Female,enter code here 
    Other 
} 
+0

我想通過departmentID與三個模型/表進行外鍵關係,但錯誤顯示如下>屬性'DepartmentID'不能被配置爲導航屬性。該屬性必須是有效的實體類型,並且該屬性應該具有非抽象的getter和setter。對於集合屬性,類型必須實現ICollection ,其中T是有效的實體類型 我的模型是 –

+1

YOu不應該在Comment中編寫您的問題。請嘗試編輯您的問題並在那裏寫下您的詳細信息 –

+0

從'public int DepartmentID {get;}中移除'[ForeignKey(「DepartmentID」)]''組; }(如果需要的話,它需要應用到公共虛擬部門部門{get; set;}屬性(但它不是你的情況)。你還需要刪除'[DatabaseGenerated(DatabaseGeneratedOption.Identity) ]也是如此。 –

回答

0

使用[ForeignKey的( 「DepartmentID的」)對你的公開虛擬部門部門{獲得;組; }屬性而不是DepartmentID屬性。因此DepartmentID將被確定爲Associated Department的一個外鍵。

即使您不需要設置ForeignKey屬性,因爲您已將DepartmentID名稱設置爲與Department關聯的對象屬性相同,默認情況下,慣例EF將DepartmentID設置爲外鍵。

請參閱以下代碼,您將獲得清晰的圖像。

public class Designation 
{ 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    [Key] 
    public int id { get; set; } 

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 

    public int DepartmentID { get; set; } 

    public bool IsActive { get; set; } 

    public string AddedBy { get; set; } 

    public DateTime AddedDate { get; set; } 
    public string UpdateBy { get; set; } 

    public DateTime UpdateDate { get; set; } 
    [ForeignKey("DepartmentID")] 
    public virtual Department Department { get; set; } 
}