实体框架:使用 Code First 方法定义关系
Entitiy Framework: Defining relations with Code First Approach
作为 Entity Framework 及其方法的新手,我有几个模型 classes,我想使用它们通过代码优先方法创建数据库表。 User
model 的 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; }
}
我已经在 ControlGroup
和 ControlPoint
class 中定义了 User
但我对如何在 User
中定义关系感到困惑 class。我是否需要为其他两个 class 中的每个 User
对象在 User
中添加 5 个属性,还是只需要一个就足够了?任何帮助将不胜感激。
- 添加到您的所有 classes ID 字段
- 为用户添加导航属性class
- 我假设您在 User 和 ControlPoint、ControlGroup 实体之间有 one-to-many 关系。
- 我从User中删除了字段UserType,因为你没有显示UserType class 声明并且没有指定它们之间的关系(我不知道该怎么办,自己解决)。
最终模型将如下所示:
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
}
作为 Entity Framework 及其方法的新手,我有几个模型 classes,我想使用它们通过代码优先方法创建数据库表。 User
model 的 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; }
}
我已经在 ControlGroup
和 ControlPoint
class 中定义了 User
但我对如何在 User
中定义关系感到困惑 class。我是否需要为其他两个 class 中的每个 User
对象在 User
中添加 5 个属性,还是只需要一个就足够了?任何帮助将不胜感激。
- 添加到您的所有 classes ID 字段
- 为用户添加导航属性class
- 我假设您在 User 和 ControlPoint、ControlGroup 实体之间有 one-to-many 关系。
- 我从User中删除了字段UserType,因为你没有显示UserType class 声明并且没有指定它们之间的关系(我不知道该怎么办,自己解决)。
最终模型将如下所示:
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
}