【发布时间】:2021-02-19 08:35:43
【问题描述】:
我已经将EntityFramework 6的项目实现到EntityFramework Core中。我必须将 EF6 关系模式迁移到 EF 核心。 我在下面找到了一些参考资料:
但对 EF-Core 中的必需-可选关系没有任何想法。
Sample1.cs:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<StudentAddress>()
.HasRequired(x => x.Student)
.WithOptional(x => x.StudentAddress);
}
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public virtual StudentAddress StudentAddress { get; set; }
}
public class StudentAddress
{
public int StudentId { get; set; }
public string State { get; set; }
public virtual Student Student { get; set; }
}
Sample2.cs:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<StudentAddress>()
.HasKey(sa => sa.StudentId);
modelBuilder.Entity<StudentAddress>()
.HasRequired(x => x.Student)
.WithOptional(x => x.StudentAddress);
}
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public virtual StudentAddress StudentAddress { get; set; }
}
public class StudentAddress
{
public int StudentId { get; set; }
public string State { get; set; }
public virtual Student Student { get; set; }
}
请有人帮助我如何在 EF-Core 中使用流利的 api 做到这一点
【问题讨论】:
-
多对多是在 EF Core 5 中引入的。docs.microsoft.com/en-us/ef/core/modeling/relationships
标签: c# entity-framework-core entity-framework-6