如何将新实体与 EF4 中的现有实体关联?
我有一个 Patient
实体(存储有关患者的最新信息)和一个 TreatmentPlanPatient
实体(患者日期的副本,就像“治疗计划”,另一个与此问题无关的实体已创建):
我是在这种情况下使用每个类型表。现在,当我尝试添加一个指向从中创建的 Patient
的新 TreatmentPlanPatient
时,EF4 实际上执行以下操作:
- 它从数据库中检索现有患者
- 创建以下副本该患者
- 为其分配一个新的 ID GUID(尽管 StoreGeneratePattern = None!)
- 将新患者插入
Patients
表 - 重写我的
TreatmentPlanPatient
实例中的 ID 以指向该新患者 - 将新的治疗计划患者插入到
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):
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:
- It retrieves the existing patient from the database
- Creates a copy of that patient
- Assigns it a new ID GUID (despite StoreGeneratedPattern = None!)
- Inserts the new patient into the
Patients
table - Rewrites the ID in my
TreatmentPlanPatient
instance to point to this new Patient - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我完全理解您想要做什么,但根据您的架构,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.