Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 在EF中填充POCO类之间的关系_Entity Framework_Entity Relationship_Poco - Fatal编程技术网

Entity framework 在EF中填充POCO类之间的关系

Entity framework 在EF中填充POCO类之间的关系,entity-framework,entity-relationship,poco,Entity Framework,Entity Relationship,Poco,我正在尝试为一个从现有数据中提取数据的项目设置种子设定。给我带来麻烦的部分是如何在导入数据时设置表之间的关系 I have three tables: 1) Patient 2) InsuranceProvider 3) Doctors 基本上,患者有一个保险提供者,每个保险提供者都有几个医生供患者选择。我已经设置了以下实体 public class Patient { public int Id {get; set;} public string Name {get; set;}

我正在尝试为一个从现有数据中提取数据的项目设置种子设定。给我带来麻烦的部分是如何在导入数据时设置表之间的关系

I have three tables:
1) Patient
2) InsuranceProvider
3) Doctors
基本上,患者有一个保险提供者,每个保险提供者都有几个医生供患者选择。我已经设置了以下实体

public class Patient
{
  public int Id {get; set;}
  public string Name {get; set;}
  public int LegacyInsuranceProviderId {get; set;}
  public int InsuranceProviderId {get; set;}
  public virtual InsuranceProvider insuranceProvider {get; set;}
}

public class InsuranceProvider
{
  public int Id {get; set;}
  public int LegacyId {get; set;}
  public string CompanyName {get; set;}
  public virtual ICollection<Patient> patients {get; set;}
  public virtual ICollection<Doctor> doctors {get; set;}
}

public class Doctor
{
  public int Id {get; set;}
  public string DoctorFullName {get; set;}
  public int LegacyInsuranceProviderIdId {get; set;}
  public int InsuranceProviderId {get; set;}
  public virtual InsuranceProvider insuranceProvider {get; set;}
}
公共类患者
{
公共int Id{get;set;}
公共字符串名称{get;set;}
public int-legacySuranceProviderId{get;set;}
公共int InsuranceProviderId{get;set;}
公共虚拟保险提供程序保险提供程序{get;set;}
}
公共类保险提供者
{
公共int Id{get;set;}
public int LegacyId{get;set;}
公共字符串CompanyName{get;set;}
公共虚拟ICollection患者{get;set;}
公共虚拟ICollection{get;set;}
}
公立医生
{
公共int Id{get;set;}
公共字符串DoctorFullName{get;set;}
public int-legacySuranceProviderIDID{get;set;}
公共int InsuranceProviderId{get;set;}
公共虚拟保险提供程序保险提供程序{get;set;}
}
这些类都有一个名为“Legacy…”的字段,它表示各个表以前的主键。我这样做是为了不丢失对关系的跟踪,因为将为每个表生成新的主键


我不知道如何填充这些类之间的关系

我觉得你的设置不错

使用的
virtual
关键字通知实体框架该字段是“导航属性”。它可以在查询时通过在两者之间构造连接来使用此信息加载数据。您所要做的就是访问联接,它将填充数据。有两种方法

让我们假设我们在一个using块(或注入类)中,该块将
db
作为DbContext的已实例化对象

第一种方法是通过延迟加载。这里的
doctorPatients
变量现在将保存该医生的患者列表。注意:如果数据库已被释放,延迟加载将导致抛出异常


您的医生课程中有一个输入错误,字段为
LegacySuranceProviderIDID
。谢谢。。。修复了这样的问题:它是否像在患者和医生之间循环一样简单,将保险提供者提取到导航属性(var provider=context.InsuranceProviders.Single(ip=>ip.LegacyId==currentPatient.legacySuranceProviderId;)中,然后保存更改?我可能会使用SQL脚本而不是Seed()来完成此操作。在我看来,您展示的是如何在填充数据后进行查询。但我不确定如何链接单独的表,因为导入时它们将具有新的标识Id。LegacyId字段具有这种关系,但我不确定如何使用新键。
var doctor = db.Set<Doctor>().Find(1);//get a Doctor by primary key
var doctorPatients = doctor.insuranceProvider.patients;
var doctorWithPatients = db.Set<Doctor>.Include(d => d.insuranceProvider.patients).Find(1);