【问题标题】:Entitiy Framework: Defining relations with Code First Approach实体框架:使用代码优先方法定义关系
【发布时间】:2016-01-18 14:43:48
【问题描述】:

作为 Entity Framework 及其方法的新手,我有几个模型类,我想用它们以代码优先的方法创建数据库表。 Usermodel 的 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; }

    }

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

【问题讨论】:

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


    【解决方案1】:
    1. 添加到您的所有课程 ID 字段
    2. 向用户类添加导航属性
    3. 我假设您在 User 和 ControlPoint、ControlGroup 实体之间存在一对多的关系。
    4. 我从User中删除了UserType字段,因为你没有显示UserType类声明,也没有指定它们之间的关系(我不知道怎么处理,你自己解决)。

    最终模型将如下所示:

    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
    }
    

    【讨论】:

      猜你喜欢
      • 2011-07-28
      • 2011-08-03
      • 2017-05-17
      • 2012-10-11
      • 2017-07-05
      • 1970-01-01
      • 2013-07-13
      • 2012-03-31
      • 1970-01-01
      相关资源
      最近更新 更多