简体   繁体   English

代码优先实体框架映射问题

[英]Code-First Entity Framework mapping issues

I have a below classes for the Code First Approach in EF6. 对于EF6中的代码优先方法,我有以下课程。

This is the master class: 这是大师班:

 public class Users
 {
        [NotMapped]
        public string Password { get; set; }
        [Key, Column(Order = 1)]
        public Guid UserId { get; set; }
        [Key, Column(Order = 2)]
        public int ITS { get; set; }
 }

This is child class: 这是儿童班:

public class Class : CommonFields
    {
        [Key]
        public int ClassId { get; set; }
        public int TeacherITS { get; set; }
        public int CoordinatorITS { get; set; }

        [ForeignKey("TeacherITS, CoordinatorITS"), Column(Order = 2)]
        public virtual Users Users { get; set; }
    }

now while trying to update the database i am getting 现在,当我尝试更新数据库时

The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'TeacherITS' on entity 'Class' does not match the type of property 'UserId' on entity 'Users' in the referential constraint 'Class_Users'

I made updates in the child class below: 我在以下子类中进行了更新:

public class Class : CommonFields
{
    [Key]
    public int ClassId { get; set; }
    [ForeignKey("Teacher"), Column(Order = 2)]
    public int TeacherITS { get; set; }
    [ForeignKey("Coordinator"), Column(Order = 2)]
    public int CoordinatorITS { get; set; }

    public virtual Users Teacher { get; set; }
    public virtual Users Coordinator { get; set; }
}

and now i am getting below error: 现在我得到以下错误:

The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical

I'm not sure to understand your logic, the 2 class have to reference each other, it's why is missing property. 我不确定您的逻辑,两个类必须互相引用,这就是为什么缺少属性。 It should be something like this : 应该是这样的:

Edit : 编辑:

public class Users
 {
     [Key, Column(Order = 1)]
     public Guid UserId { get; set; }

     [Key, Column(Order = 2)]
     public int ITS { get; set; }

     [NotMapped]
     public string Password { get; set; }

     // Reference to Class.

     [ForeignKey("TeacherITS", "CoordinatorITS")]
     public ICollection<Class> Class { get; set; }
}


public class Class : CommonFields
{
    [Key, Column(Order = 1)]
    public int ClassId { get; set; }


    [Column(Order = 2)]
    public int TeacherITS { get; set; }

    [Column(Order = 3)]
    public int CoordinatorITS { get; set; }

    public Users Users { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM