【问题标题】:How do I make a self reference in Entity Framework Code First?如何在 Entity Framework Code First 中进行自我引用?
【发布时间】:2013-05-22 00:25:38
【问题描述】:

我有一个设备类、一个计算机类和一个虚拟桌面类。每台计算机都是一台设备,每台虚拟桌面都是一台计算机。我想向我的虚拟桌面添加一个属性,该属性引用另一个计算机类型的设备,指示虚拟设备的托管位置。我在 Code First 环境中使用带有 Entity Framework 5.0 的 POCO。我应该如何将此属性添加到下面的虚拟桌面类中?这是自引用关系吗?

[Table("devices")]
public class device 
{
   public int DeviceId { get; set; }
   public string Description { get; set; }
   public string IPAddress { get; set; }
}

[Table("computers")]
public class computer : device
{
    public string OperatingSystem { get; set; }
    public string OS_LicenseKey { get; set; }
}

[Table("virtualdesktops")]
public class virtualdesktop : computer
{
 //Should I do this
  public Computer Computer {get; set;}

 //Or should I do this
  public int ComputerId {get; set;}
}

【问题讨论】:

    标签: entity-framework entity-framework-5 poco


    【解决方案1】:

    两者都需要。

    阅读this 了解更多详细信息,包括如何设置可选关系和多对一关系。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-08
      • 2015-04-15
      • 2013-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      相关资源
      最近更新 更多