EF 代码优先:设置可选的与数据注释的一对一关系
EF Code first : set optional one to one relationship with data annotation
我尝试解决以下情况:我有 2 个 table,一个带有一些字段的课程 table 和一个可选的 CourseDescription table(所以课程 可以 有一个 CourseDescription 但 CourseDescription 必须 有一个课程)。我正在尝试设置它。到目前为止,这是我所拥有的:
public class Course
{
[Key, Column("Key_Course")]
public int ID { get; set; }
public string Name { get; set; }
public virtual CourseDescription CourseDescription { get; set; }
}
public class CourseDescription
{
[Key, ForeignKey("Course")]
public int ID { get; set; }
public string Description { get; set; }
public string PreRequis { get; set; }
public int CoursesID { get; set; }
[ForeignKey("CoursesID")]
public Course Course { get; set; }
}
这 "works" 意味着 EF 没有抱怨我的模型,但关系没有正确完成,因为 EF 将 CourseDescription 的 PK 与 Course 的 PK 相关联。在我的数据库中,情况并非如此(例如:CourseDescription.ID=1 与 CourseDescription.CoursesID=3 关联,而不是 1)。
有没有办法通过数据注释来解决这个问题?我知道我可以使用流利的 API 但我不想为此覆盖模型构建(除非没有其他方法)。
谢谢
您不应该为 CourseDescription class 的 ID 属性 使用 ForeignKey 属性,因为您不想在主键之间建立关联。尝试删除它。
编辑:看来我上次的问题理解错了。
您可以这样获得您的 CourseDescription。
public class CourseDescription
{
[Key, ForeignKey("Course")]
public int ID { get; set; }
public string Description { get; set; }
public string PreRequis { get; set; }
public Course Course { get; set; }
}
在这种情况下,您不需要 CoursesID 字段。实体将通过主键连接。
嗯,我想你有两个选择:
配置一对多关系
如果想映射Course
和CourseDescription
关系的外键,又不想声明外键属性为[=13]的Key =] 实体,那么,您没有其他选择来配置一对多关系。在那种情况下,您的模型将是这样的:
public class Course
{
[Key, Column("Key_Course")]
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<CourseDescription> CourseDescriptions { get; set;}
}
public class CourseDescription
{
[Key]
public int ID { get; set; }
public string Description { get; set; }
public string PreRequis { get; set; }
[ForeignKey("Course")]
public int CourseID { get; set; }
public Course Course { get; set; }
}
配置一对一关系但不映射
关系
EF 允许您以一对一关系映射 FK 的唯一方法是当 FK 也被声明为 PK 时,因此如果您希望在两个实体中具有不同的 ID 并且您想要稳定一对一的关系,那么你可以这样做:
public class Course
{
[Key, Column("Key_Course")]
public int ID { get; set; }
public string Name { get; set; }
public CourseDescription CourseDescription { get; set;}
}
public class CourseDescription
{
[Key]
public int ID { get; set; }
public string Description { get; set; }
public string PreRequis { get; set; }
[Required]
public Course Course { get; set; }
}
并使用导航属性。
我尝试解决以下情况:我有 2 个 table,一个带有一些字段的课程 table 和一个可选的 CourseDescription table(所以课程 可以 有一个 CourseDescription 但 CourseDescription 必须 有一个课程)。我正在尝试设置它。到目前为止,这是我所拥有的:
public class Course
{
[Key, Column("Key_Course")]
public int ID { get; set; }
public string Name { get; set; }
public virtual CourseDescription CourseDescription { get; set; }
}
public class CourseDescription
{
[Key, ForeignKey("Course")]
public int ID { get; set; }
public string Description { get; set; }
public string PreRequis { get; set; }
public int CoursesID { get; set; }
[ForeignKey("CoursesID")]
public Course Course { get; set; }
}
这 "works" 意味着 EF 没有抱怨我的模型,但关系没有正确完成,因为 EF 将 CourseDescription 的 PK 与 Course 的 PK 相关联。在我的数据库中,情况并非如此(例如:CourseDescription.ID=1 与 CourseDescription.CoursesID=3 关联,而不是 1)。
有没有办法通过数据注释来解决这个问题?我知道我可以使用流利的 API 但我不想为此覆盖模型构建(除非没有其他方法)。
谢谢
您不应该为 CourseDescription class 的 ID 属性 使用 ForeignKey 属性,因为您不想在主键之间建立关联。尝试删除它。
编辑:看来我上次的问题理解错了。
您可以这样获得您的 CourseDescription。
public class CourseDescription
{
[Key, ForeignKey("Course")]
public int ID { get; set; }
public string Description { get; set; }
public string PreRequis { get; set; }
public Course Course { get; set; }
}
在这种情况下,您不需要 CoursesID 字段。实体将通过主键连接。
嗯,我想你有两个选择:
配置一对多关系
如果想映射
Course
和CourseDescription
关系的外键,又不想声明外键属性为[=13]的Key =] 实体,那么,您没有其他选择来配置一对多关系。在那种情况下,您的模型将是这样的:public class Course { [Key, Column("Key_Course")] public int ID { get; set; } public string Name { get; set; } public virtual ICollection<CourseDescription> CourseDescriptions { get; set;} } public class CourseDescription { [Key] public int ID { get; set; } public string Description { get; set; } public string PreRequis { get; set; } [ForeignKey("Course")] public int CourseID { get; set; } public Course Course { get; set; } }
配置一对一关系但不映射 关系
EF 允许您以一对一关系映射 FK 的唯一方法是当 FK 也被声明为 PK 时,因此如果您希望在两个实体中具有不同的 ID 并且您想要稳定一对一的关系,那么你可以这样做:
public class Course { [Key, Column("Key_Course")] public int ID { get; set; } public string Name { get; set; } public CourseDescription CourseDescription { get; set;} } public class CourseDescription { [Key] public int ID { get; set; } public string Description { get; set; } public string PreRequis { get; set; } [Required] public Course Course { get; set; } }
并使用导航属性。