修改一对多关系 - Code First
我认为以下任务看起来很容易编写代码,但我花了很多时间,但尚未找到解决它的正确方法。 场景
这是我有三个实体城市、地点和捐助者的 。城市有许多地区,每个地区与捐助者有两个一对多的关系,如下所示。
public class City {
public virtual List<Locality> Localities { get; set; }
}
public class Locality {
public virtual List<Donor> DonorsInOfficeLocality { get; set; }
public virtual List<Donor> DonorsInResidenceLocality { get; set; }
}
public class Donor : User {
public virtual Locality OfficeLocality {get;set;}
public virtual Locality ResidenceLocality { get; set; }
}
在我的捐赠者编辑模块中,我收到(捐赠者 ID、residenceLocalityId 和 officeLocalityId)。施主实体处于来自表示层的分离状态。这段代码不起作用。旧有的关系仍然完好无损。
public void EditDonor(Donor donor, int residenceLocalityId, int officeLocalityId) {
Locality residenceLocality = context.Localities.Find(residenceLocalityId);
donor.ResidenceLocality = residenceLocality;
Locality officeLocality = context.Localities.Find(officeLocalityId);
donor.OfficeLocality = officeLocality;
context.Donors.Attach(donor);
context.Entry(donor).State = EntityState.Modified;
context.SaveChanges();
}
我尝试的另一件事是将 ResidenceLocality 和 OfficeLocality 的状态更改为 Modified,但这也不起作用。旧有的关系仍然完好无损。在这种情况下,由于我将 Locality 更改为 Modified,因此我必须额外加载 City 以避免修改后的 locality 出现更新异常。
如果有人可以让我知道如何修改从表示层接收到的处于分离状态的实体的一对多关系(如上所述)。
I thought the following task would be seemingly easy to code, but I have spent a lot of time and not yet figured out the right way to solve it. Here is the scenario
I have three Entities City, Locality and Donor. City has many localities and each Locality has two One to Many relationship with donor as given below.
public class City {
public virtual List<Locality> Localities { get; set; }
}
public class Locality {
public virtual List<Donor> DonorsInOfficeLocality { get; set; }
public virtual List<Donor> DonorsInResidenceLocality { get; set; }
}
public class Donor : User {
public virtual Locality OfficeLocality {get;set;}
public virtual Locality ResidenceLocality { get; set; }
}
In my Donor edit module I receive (Donor Id, residenceLocalityId and officeLocalityId). The Donor entity is in detached state comming from presentation layer. This code does not work. The old relationships remain intact.
public void EditDonor(Donor donor, int residenceLocalityId, int officeLocalityId) {
Locality residenceLocality = context.Localities.Find(residenceLocalityId);
donor.ResidenceLocality = residenceLocality;
Locality officeLocality = context.Localities.Find(officeLocalityId);
donor.OfficeLocality = officeLocality;
context.Donors.Attach(donor);
context.Entry(donor).State = EntityState.Modified;
context.SaveChanges();
}
One more thing I tried is changing the state of ResidenceLocality and OfficeLocality to Modified, but that didn't work either. The old relations remained intact. And in this case, since I change the Locality as Modified, I have to additionally load City to avoid Update Exception for the modified locality.
If someone can let me know how to modify One to Many relations (as above) for entity in detached state received from presentation layer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在模型中保留导航属性和外键。
如果您有外键属性,则不需要通过 id 查找来更新导航属性,
you can keep both navigation property and the foreign key in your model.
if you have the foreign key properties you don't need to find by id to update navigation properties,