[英]make both models aware of each other with a one-to-one relationship in asp.net entity framework
I am unable to get the one to one relationship with EF to work properly. 我无法与EF建立一对一的关系以正常工作。 I've scored blogs, SO, and msdn docs, but nothing I do seems to work. 我已经给博客,SO和msdn文档打了分,但我似乎没什么用。
I have two models, a Class and an Exam that look like the following: 我有两个模型,一个类和一个考试,如下所示:
[Table("classes")]
public class Class
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[StringLength(255), Required]
public string Title { get; set; }
public virtual Exam Exam { get; set; }
}
[Table("exams")]
public class Exam
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[DisplayFormat(DataFormatString = "{0:h:mm tt}")]
public DateTime? Time { get; set; }
public int ClassId { get; set; }
public virtual Class Class { get; set; }
}
I want to be able to access the exam from the Class and the Class from the Exam, but no matter what I do, I find some error. 我希望能够从班级中访问考试,并从考试中访问班级,但是无论我做什么,我都会发现一些错误。
Trying to create/run migrations I get the following. 尝试创建/运行迁移,我得到以下信息。
The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
If I add this to my context: 如果我将其添加到我的上下文中:
protected override void OnModelCreating(DbModelBuilder builder)
{
builder.Entity<Exam>()
.HasRequired(e => e.Class)
.WithOptional(c => c.Exam);
}
I get the following error: 我收到以下错误:
System.Data.Entity.Infrastructure.DbUpdateException: Entities in 'BenchContext.Exams' participate in the 'Class_Exam' relationship. 0 related 'Class_Exam_Source' were found. 1 'Class_Exam_Source' is expected. ---> System.Data.UpdateException: Entities in 'BenchContext.Exams' participate in the 'Class_Exam' relationship. 0 related 'Class_Exam_Source' were found. 1 'Class_Exam_Source' is expected.
I'm not sure how to tell the fluent api how to correctly foreign key between the two models and nothing I do seems to affect it. 我不确定如何告诉流利的api如何正确地在两个模型之间外键,我似乎没有任何影响。
What am I missing? 我想念什么?
You are trying to build one-to-one relation through ClassId
property in Exam
class. 您正在尝试通过Exam
类中的ClassId
属性建立一对一的关系。 That requires ClassId
to be unique (= unique constraint) but unique constraint are not supported by EF yet. 这要求ClassId
是唯一的(=唯一约束),但是EF尚不支持唯一约束。
The only way EF currently supports real one-to-one relation is by sharing primary key: EF当前支持真实一对一关系的唯一方法是共享主键:
[Table("classes")]
public class Class
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[StringLength(255), Required]
public string Title { get; set; }
public virtual Exam Exam { get; set; }
}
[Table("exams")]
public class Exam
{
[Key, ForeignKey("Class")]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
public int Id { get; set; }
[DisplayFormat(DataFormatString = "{0:h:mm tt}")]
public DateTime? Time { get; set; }
public virtual Class Class { get; set; }
}
The Id
in Exams
table is both PK and FK to Classes
table. Exams
中的Id
表是PK和FK到Classes
表。 It cannot have autogenerated value. 它不能具有自动生成的值。 While this is one-to-one relation it still has 1 - 0..1 multiplicity ( Class
can exist without Exam
but Exam
must have Class
). 尽管这是一对一的关系,但它仍然具有1-0..1的多重性( Class
可以不通过Exam
而存在,但是Exam
必须具有Class
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.