Entitiy Framework:使用Code First方法定义关系

时间:2016-01-18 14:43:48

标签: database-design ef-code-first entity-framework-6 code-first

作为Entity Framework及其方法的新手,我有几个模型类,我想用它们来创建具有代码优先方法的数据库表。 User模型的id指的是2个表,每个表至少有2列,如下所示。

    public class ControlGroup
    {

        public int ControlGroupId { get; set; }

        public string ControlGroupName { get; set; }

        public virtual User CreatedBy { get; set; }

        public DateTime CreationTime { get; set; }

        public virtual User UpdatedBy { get; set; }

        public DateTime UpdateTime { get; set; }

    }

    public class ControlPoint
    {

        public int ControlPointId { get; set; }

        public string ControlPointName { get; set; }

        public virtual User CreatedBy { get; set; }

        public DateTime CreationTime { get; set; }

        public virtual User UpdatedBy { get; set; }

        public virtual User Auditor{ get; set; }

        public DateTime UpdateTime { get; set; }

    }

    public class User
    {

        public string UserId { get; set; }

        public string UserFirstName { get; set; }

        public string UserLastName { get; set; }

        public string UserPassword { get; set; }

        public string UserEmail { get; set; }

        public virtual UserType UserType { get; set; }

    }

我已经在UserControlGroup类中定义了ControlPoint,但我对如何在User类中定义关系感到困惑。我是否需要在User中为其他两个类中的每个User对象放置5个attiributes,或者只需要一个就足够了?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

  1. 添加到您的所有课程ID字段
  2. 向用户类添加导航属性
  3. 我假设您在User和ControlPoint,ControlGroup实体之间存在一对多的关系。
  4. 我从User中删除了字段UserType,因为您没有显示UserType类声明,也没有指定它们之间的关系(我不知道如何处理它,请自行解决)。
  5. 最后模型将如下所示:

    public class ControlGroup
    {
        //was added
        public int ID { get; set; }
    
        public int ControlGroupId { get; set; }    
        public string ControlGroupName { get; set; }    
        public virtual User CreatedBy { get; set; }    
        public DateTime CreationTime { get; set; }    
        public virtual User UpdatedBy { get; set; }    
        public DateTime UpdateTime { get; set; }    
    }
    
    public class ControlPoint
    {
        //was added
        public int ID { get; set; }
    
        public int ControlPointId { get; set; }    
        public string ControlPointName { get; set; }    
        public virtual User CreatedBy { get; set; }    
        public DateTime CreationTime { get; set; }    
        public virtual User UpdatedBy { get; set; }    
        public virtual User Auditor { get; set; }    
        public DateTime UpdateTime { get; set; }    
    }
    
    public class User
    {
        //was added
        public int ID { get; set; }
    
        public string UserId { get; set; }    
        public string UserFirstName { get; set; }    
        public string UserLastName { get; set; }    
        public string UserPassword { get; set; }    
        public string UserEmail { get; set; }
    
        //on your own
        //public virtual UserType UserType { get; set; }
    
        //navigation properties one-to-many were added
        [InverseProperty("Auditor")]
        public virtual ICollection<ControlPoint> ControlPointAuditor { get; set; }
        [InverseProperty("UpdatedBy")]
        public virtual ICollection<ControlPoint> ControlPointUpdatedBy { get; set; }
        [InverseProperty("CreatedBy")]
        public virtual ICollection<ControlPoint> ControlPointCreatedBy { get; set; }    
        [InverseProperty("UpdatedBy")]
        public virtual ICollection<ControlGroup> ControlGroupUpdatedBy { get; set; }
        [InverseProperty("CreatedBy")]
        public virtual ICollection<ControlGroup> ControlGroupCreatedBy { get; set; }
        //navigation one-to-many
    }