作为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; }
}
我已经在User
和ControlGroup
类中定义了ControlPoint
,但我对如何在User
类中定义关系感到困惑。我是否需要在User
中为其他两个类中的每个User
对象放置5个attiributes,或者只需要一个就足够了?任何帮助将不胜感激。
答案 0 :(得分:0)
最后模型将如下所示:
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
}