Entity Framework 只有一个导航的一对多 属性:WithRequiredDependant?
Entity Framework One-To-Many with only one navigation property: WithRequiredDependant?
使用最新的 Entity Framework 我有一个 class 具有一对多的机智,在多边上只有一个导航 属性。
如MSDN: Entity Framework Fluent API - Relationships所述:
A one-directional (also called unidirectional) relationship is when a
navigation property is defined on only one of the relationship ends
and not on both.
简化:一个School
有很多Students
; School 和 Student 之间存在一对多关系,但 School 没有包含 Students
集合的 属性
class Student
{
public int Id {get; set;}
// a Student attends one School; foreign key SchoolId
public int SchoolId {get; set;}
public School School {get; set;}
}
class School
{
public int Id {get; set;}
// missing: public virtual ICollection<Studen> Students {get; set;}
}
在双向关系中,你可以在OnModelCreating
中写出如下流利的API:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>()
.HasRequired(student => student.School)
.WithMany(school => school.Students)
.HasForeignKey(student => student.SchoolId);
}
由于缺少School.Students
,我需要做一些额外的事情。根据开头的 link 看来我必须对 WithRequiredDependant
做点什么。
// Summary:
// Configures the relationship to be required without a navigation property
// on the other side of the relationship. The entity type being configured will
// be the dependent and contain a foreign key to the principal. The entity type
// that the relationship targets will be the principal in the relationship.
//
public ForeignKeyNavigationPropertyConfiguration WithRequiredDependent();
modelBuilder.Entity<Student>()
.HasRequired(student => student.School)
.WithRequiredDependent();
唉,这不行。 SchoolId 未建模为外键。
我需要什么流利的API?
我希望我的想法是正确的version/edition:
modelBuilder.Entity<Student>()
.HasRequired(student => student.School)
//.WithMany(school => school.Students)
.WithMany()
.HasForeignKey(student => student.SchoolId);
使用最新的 Entity Framework 我有一个 class 具有一对多的机智,在多边上只有一个导航 属性。
如MSDN: Entity Framework Fluent API - Relationships所述:
A one-directional (also called unidirectional) relationship is when a navigation property is defined on only one of the relationship ends and not on both.
简化:一个School
有很多Students
; School 和 Student 之间存在一对多关系,但 School 没有包含 Students
class Student
{
public int Id {get; set;}
// a Student attends one School; foreign key SchoolId
public int SchoolId {get; set;}
public School School {get; set;}
}
class School
{
public int Id {get; set;}
// missing: public virtual ICollection<Studen> Students {get; set;}
}
在双向关系中,你可以在OnModelCreating
中写出如下流利的API:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>()
.HasRequired(student => student.School)
.WithMany(school => school.Students)
.HasForeignKey(student => student.SchoolId);
}
由于缺少School.Students
,我需要做一些额外的事情。根据开头的 link 看来我必须对 WithRequiredDependant
做点什么。
// Summary:
// Configures the relationship to be required without a navigation property
// on the other side of the relationship. The entity type being configured will
// be the dependent and contain a foreign key to the principal. The entity type
// that the relationship targets will be the principal in the relationship.
//
public ForeignKeyNavigationPropertyConfiguration WithRequiredDependent();
modelBuilder.Entity<Student>()
.HasRequired(student => student.School)
.WithRequiredDependent();
唉,这不行。 SchoolId 未建模为外键。
我需要什么流利的API?
我希望我的想法是正确的version/edition:
modelBuilder.Entity<Student>()
.HasRequired(student => student.School)
//.WithMany(school => school.Students)
.WithMany()
.HasForeignKey(student => student.SchoolId);