属性“DepartmentID”无法配置为导航属性

时间:2016-11-09 04:41:23

标签: c# asp.net-mvc entity-framework code-first foreign-key-relationship

指定课程

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
}

1 个答案:

答案 0 :(得分:0)

[ForeignKey("DepartmentID")]媒体资源而不是public virtual Department Department { get; set; }媒体资源上使用DepartmentID。因此DepartmentID将被识别为关联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; }
}