目前我正在尝试制作一个 EF 数据库,我想知道为什么以及如何获得它 "NULL" 远离我的 table

Currently i am trying tomake a EF database and I wunder why and how to get this is "NULL" away from my table

我目前正在尝试用一些 relations/FKs.

制作一个 EF 系统

我的 class 看起来像这样: 我有一个 artikel class、一个 Location class 和一个 prognoseclass,如下所示:

在我 运行 我的 "Package manager comandline" 和 "fresh database" 之后,我得到了像这样的种子数据:

现在的问题是:为什么 "Entry" Location_LocationId 有一个 NULL 值?

如何才能把它拿走或者它应该在那里:??

如果我没有提供足够的数据,请告诉我。

我的系统正在按照我想要的方式运行,但我非常想 "optimize" 它并试图解决这样的问题。

尝试使用属性 ForeignKey。

[ForeignKey("Location")]

在"LocationId"之前属性在"Prognose"class

您可以阅读本教程http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx 如果你想在EF中建立一对一的关系。

Prognose 上的 [Key] 应该在 PrognoseId 上,而不是 LocationId。 位置也应该是 public 虚拟 属性:

public class Prognose
{
  [Key]
  public int PrognoseId {get;set;}
  // ..
  [ForeignKey("Location")]
  public int LocationId {get;set;}
  public virtual Location Location {get;set;}
}

我找到了解决方案:

在我的 class "locacaion" 中,我没有 nullabale 值,这在某种程度上是解决方案。

和预后 class 看你建议的变化:

再次感谢您的帮助和链接:-)

祝您编码愉快。