【问题标题】:Fluent API EF tutorial mistakeFluent API EF 教程错误
【发布时间】:2016-05-24 12:16:48
【问题描述】:

我指的是这里的教程:

http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx

给定

public class Student
{
  public int StudentId { get; set; }
  public string StudentName { get; set; }

  //StdId is not following code first conventions name
  public int StdId { get; set; }

  public virtual Standard Standard { get; set; }
}

public class Standard
{
  public int StandardId { get; set; }
  public string Description { get; set; }

  public virtual ICollection<Student> Students { get; set; }
}

如果我错了,请纠正我。我认为这是错误的

modelBuilder.Entity<Standard>()
  .HasMany<Student>(s => s.Students)
  .WithRequired(s => s.Standard)
  .HasForeignKey(s => s.StdId);

这是正确的

modelBuilder.Entity<Student>()
  .HasRequired<Standard>(s => s.Standard)
  .WithMany(s => s.Students)
  .HasForeignKey(s => s.StdId);

因为 StdId 是 Student 的外键,而不是 Standard。

但是文章说它们是一样的。

如果我是对的,请告诉我。

谢谢。

【问题讨论】:

    标签: c# entity-framework-6 extension-methods ef-fluent-api


    【解决方案1】:

    玩弄代码后弄明白了。

    这两个fluent API是一样的,从Student还是Standard遍历都无所谓。

    原因是这里只有一个外键,那就是 StdId。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      • 2018-08-18
      相关资源
      最近更新 更多