C# Fluent API EF教程错误
我指的是这里的教程: 给定C# Fluent API EF教程错误,c#,entity-framework-6,extension-methods,ef-fluent-api,C#,Entity Framework 6,Extension Methods,Ef Fluent Api,我指的是这里的教程: 给定 公共班级学生 { 公共int StudentId{get;set;} 公共字符串StudentName{get;set;} //StdId未遵循代码优先约定名称 公共int StdId{get;set;} 公共虚拟标准{get;set;} } 公共类标准 { 公共int标准ID{get;set;} 公共字符串说明{get;set;} 公共虚拟ICollection学生{get;set;} } 如果我错了,请纠正我。我认为这是错误的 modelBuilder.Ent
公共班级学生
{
公共int StudentId{get;set;}
公共字符串StudentName{get;set;}
//StdId未遵循代码优先约定名称
公共int StdId{get;set;}
公共虚拟标准{get;set;}
}
公共类标准
{
公共int标准ID{get;set;}
公共字符串说明{get;set;}
公共虚拟ICollection学生{get;set;}
}
如果我错了,请纠正我。我认为这是错误的
modelBuilder.Entity<Standard>()
.HasMany<Student>(s => s.Students)
.WithRequired(s => s.Standard)
.HasForeignKey(s => s.StdId);
modelBuilder.Entity()
.有许多(s=>s.学生)
.带必填项(s=>s.标准)
.HasForeignKey(s=>s.StdId);
这是正确的
modelBuilder.Entity<Student>()
.HasRequired<Standard>(s => s.Standard)
.WithMany(s => s.Students)
.HasForeignKey(s => s.StdId);
modelBuilder.Entity()
.HasRequired(s=>s.Standard)
.有许多(s=>s.学生)
.HasForeignKey(s=>s.StdId);
因为StdId是学生的外键,不是标准的
但是文章说他们是一样的
请让我知道我是否正确
谢谢。在反复使用代码后,我们找到了答案 这两个fluentapi是相同的,无论您是从Student还是从Standard进行遍历都没有关系
原因是这里只有一个外键,那就是StdId。在处理代码后找到了它 这两个fluentapi是相同的,无论您是从Student还是从Standard进行遍历都没有关系 原因是这里只有一个外键,那就是StdId
modelBuilder.Entity<Student>()
.HasRequired<Standard>(s => s.Standard)
.WithMany(s => s.Students)
.HasForeignKey(s => s.StdId);