如何将新实体与 EF4 中的现有实体关联?

发布于 2024-10-24 23:56:46 字数 1500 浏览 1 评论 0原文

我有一个 Patient 实体(存储有关患者的最新信息)和一个 TreatmentPlanPatient 实体(患者日期的副本,就像“治疗计划”,另一个与此问题无关的实体已创建):

我的患者实体的 EF4 ORM 模型

我是在这种情况下使用每个类型表。现在,当我尝试添加一个指向从中创建的 Patient 的新 TreatmentPlanPatient 时,EF4 实际上执行以下操作:

  1. 它从数据库中检索现有患者
  2. 创建以下副本该患者
  3. 为其分配一个新的 ID GUID(尽管 StoreGeneratePattern = None!)
  4. 将新患者插入 Patients
  5. 重写我的 TreatmentPlanPatient 实例中的 ID 以指向该新患者
  6. 将新的治疗计划患者插入到 TreatmentPlanPatients 表中。

这是我的代码导致上述行为:

using(var container = new SmartTherapyContainer()) {

  // Look up the patient in the current container to make sure EF4 recognizes it
  var patient = container.Patients.First(r => r.Id == selectedPatient.Id);

  var treatmentPlanPatient = new TreatmentPlanPatient() {
    Id = Guid.NewGuid(),
    FirstName = patient.FirstName,
    LastName = patient.LastName,
    Street = patient.Street,
    ZipCode = patient.ZipCode,
    City = patient.City,
    BirthDate = patient.BirthDate,
    Telephone = patient.Telephone,
    Email = patient.Email,
    ClonedPatient = patient
  };

  // EF4 doesn't generate a separate .TreatmentPlanPatients collection :-/
  container.Patients.AddObject(treatmentPlanPatient);
  container.SaveChanges();

}

发生了什么事?如何让 EF4 将治疗计划患者插入到 TreatmentPlanPlanPatients 表中,并将其与 Patients 表中的现有患者关联起来?

I've got a Patient entity (storing up-to-date information about a patient) and a TreatmentPlanPatient entity (a copy of a patient's date as it was when the "treatment plan", another entity not relevant to this question, was created):

EF4 ORM model for my Patient entities

I'm using Table-per-Type in this case. Now when I try to add a new TreatmentPlanPatient that's pointing to the Patient it was created from, EF4 actually does this:

  1. It retrieves the existing patient from the database
  2. Creates a copy of that patient
  3. Assigns it a new ID GUID (despite StoreGeneratedPattern = None!)
  4. Inserts the new patient into the Patients table
  5. Rewrites the ID in my TreatmentPlanPatient instance to point to this new Patient
  6. Inserts the new treatment plan patient in to the TreatmentPlanPatients table.

This is my code causing above behavior:

using(var container = new SmartTherapyContainer()) {

  // Look up the patient in the current container to make sure EF4 recognizes it
  var patient = container.Patients.First(r => r.Id == selectedPatient.Id);

  var treatmentPlanPatient = new TreatmentPlanPatient() {
    Id = Guid.NewGuid(),
    FirstName = patient.FirstName,
    LastName = patient.LastName,
    Street = patient.Street,
    ZipCode = patient.ZipCode,
    City = patient.City,
    BirthDate = patient.BirthDate,
    Telephone = patient.Telephone,
    Email = patient.Email,
    ClonedPatient = patient
  };

  // EF4 doesn't generate a separate .TreatmentPlanPatients collection :-/
  container.Patients.AddObject(treatmentPlanPatient);
  container.SaveChanges();

}

What is going on? How can I get EF4 to just insert a treatment plan patient into the TreatmentPlanPatients table and associate it with the existing patient in the Patients table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安人多梦 2024-10-31 23:56:46

我不确定我完全理解您想要做什么,但根据您的架构,Patient 表是TreatmentPlanPatients 表的父级(一对多关系)。因此,您应该将治疗计划添加到患者本身而不是患者表中。因此,您不应该执行container.Patients.AddObject(treatmentPlanPatient),而应该执行patent.TreatmentPlanPatient.Add(treatmentPlanPatient)。在这种情况下,TreatmentPlanPatients 应该是 Patient 表/实体上的导航属性。

或者,如果您确实想使用继承,那么您必须在两个表中使用相同的主键(在本例中为 Id)并具有一对一的关系。

I am not sure I completely understand what you are trying to do but according to your schema, the Patient table is a parent of TreatmentPlanPatients table (one to many relationship). Hence, you should add the TreatmentPlan to the Patient itself and not the Patients table. So rather than doing container.Patients.AddObject(treatmentPlanPatient), you should do patient.TreatmentPlanPatients.Add(treatmentPlanPatient). In this case, TreatmentPlanPatients should be a navigational property on the Patient table/entity.

OR, if you really want to use inheritance, then you have to use the same primary key (Id in this case) in both tables and have a one-to-one relationship.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文