如何使用第一个 ObjectContext 上的删除来更新第二个 ObjectContext

发布于 2024-10-06 09:44:52 字数 1702 浏览 0 评论 0原文

我有一个多对多关系患者 - PatientDevices - 设备和它的基本 edmx 模型(没有 poco,自动生成)。 PatientDevices 生成为实体,因为它的列数多于外键。

当我创建两个 ObjectContext 并将新的 PatientDevice 添加到第一个 ObjectContext 中时,第二个 ObjectContext 也有它。当从第一个关系中删除该关系时,它仍然在第二个上下文中:

var entities1 = new TherapyDatabaseDevEntities();
var entities2 = new TherapyDatabaseDevEntities();

entities1.PatientDevices.AddObject(new PatientDevice
{
    Patient = entities1.Patients.First(),
    Device = entities1.Devices.First()
});
entities1.SaveChanges();

var relation1a = entities1.Patients.First().PatientDevices.ToList();
var relation2a = entities2.Patients.First().PatientDevices.ToList();

entities1.PatientDevices.DeleteObject(entities1.PatientDevices.ToList().Last());
entities1.SaveChanges();

var relation1b = entities1.Patients.First().PatientDevices.ToList();
var relation2b = entities2.Patients.First().PatientDevices.ToList();

relation1a 和relation2a 都有一个条目。 relation1b 没有条目,但relation2b 有一个条目。即使在查询之前使用刷新:

entities2.Refresh(RefreshMode.StoreWins, entities2.Patients);
entities2.Refresh(RefreshMode.StoreWins, entities2.PatientDevices);
entities2.Refresh(RefreshMode.StoreWins, entities2.Devices);
var relation1b = entities1.Patients.First().PatientDevices.ToList();
// still 1 entry
var relation2b = entities2.Patients.First().PatientDevices.ToList();

是否有可能使第二个上下文保持最新,或者我是否必须创建另一个 ObjectContext?

编辑

我发现如果我这样做:

entities2.Refresh(RefreshMode.StoreWins, entities2.Patients.First().PatientDevices);

关系就会正确更新。遗憾的是,如果没有刷新,entities2.PatientDevices 不再包含已删除的对象,但Entity2.Patients.First().PatientDevices 仍然包含它。

这是有意的行为吗?

I have a many-to-many relation Patients - PatientDevices - Devices and a basic edmx-model of it (no poco, automatic generation). PatientDevices is generated as an entity, because it has more columns than the foreign keys.

When I create two ObjectContexts and add a new PatientDevice into the first one, the second one has it also. When deleting this relation from the first one, it is still in the second context:

var entities1 = new TherapyDatabaseDevEntities();
var entities2 = new TherapyDatabaseDevEntities();

entities1.PatientDevices.AddObject(new PatientDevice
{
    Patient = entities1.Patients.First(),
    Device = entities1.Devices.First()
});
entities1.SaveChanges();

var relation1a = entities1.Patients.First().PatientDevices.ToList();
var relation2a = entities2.Patients.First().PatientDevices.ToList();

entities1.PatientDevices.DeleteObject(entities1.PatientDevices.ToList().Last());
entities1.SaveChanges();

var relation1b = entities1.Patients.First().PatientDevices.ToList();
var relation2b = entities2.Patients.First().PatientDevices.ToList();

relation1a and relation2a both have one entry. relation1b has no entry, but relation2b has one entry. Even if working with refreshes before the query:

entities2.Refresh(RefreshMode.StoreWins, entities2.Patients);
entities2.Refresh(RefreshMode.StoreWins, entities2.PatientDevices);
entities2.Refresh(RefreshMode.StoreWins, entities2.Devices);
var relation1b = entities1.Patients.First().PatientDevices.ToList();
// still 1 entry
var relation2b = entities2.Patients.First().PatientDevices.ToList();

Is there a possibility to bring the second context up to date or do I have to create another ObjectContext?

Edit

I found out that if I do this:

entities2.Refresh(RefreshMode.StoreWins, entities2.Patients.First().PatientDevices);

the relation gets updated properly. It's a pity that without the refresh entities2.PatientDevices does not contain the deleted object anymore, but entities2.Patients.First().PatientDevices still has it.

Is this intended behavior?

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

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

发布评论

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

评论(1

合久必婚 2024-10-13 09:44:52

如果您必须有多个上下文并直接使用您的实体,请查看 Attach 和 Detach 方法,顾名思义,它们用于将对象与从中检索对象的上下文关联/分离。但请注意,这些方法仅分离您作为参数传递的对象,而不分离关联的对象,因此您可能必须遍历连接的对象来分离每个对象,这很混乱。

    var entities1 = new TherapyDatabaseDevEntities();
    var patient1 = entities1.Patients.Single(p => p.Id = 12345);
entities1.Detach(patient1);
//loop through associated entities calling Detach on each

    var entities2 = new TherapyDatabaseDevEntities();
entities2.Attach(patient1);
//loop through associated entities calling Attach on each

我的偏好是使用视图模型,这样您就不会直接编辑实体,而是编辑它们的表示。当用户显式保存对象时,检索该对象并仅在新上下文中更新该对象的更改。

var entities1 = new TherapyDatabaseDevEntities();
var patient1 = entities1.Patients.Single(p => p.Id = 12345);

...处理您的上下文,它不再需要,并在此处进行更改

var entities2 = new TherapyDatabaseDevEntities();
var patient2 = entities2.Patients.Single(p => p.Id = 12345);

patient2.Property1 = patient1.Property1;

...更新其他更改(有方法使此代码更清晰,仅显示最简单的示例)

entities2.SaveChanges();
entities2.Dispose();

此处有关附加/分离的一些参考资料 - http://msdn.microsoft.com/en-us/library/bb896271.aspx

搜索实体框架上下文生命周期,关于这个主题有很多讨论,这可能会帮助您决定适合您需求的路线。

If you must have multiple contexts and work directly with your entities, have a look at the Attach and Detach methods, which as the names suggest are used to associate/dissociate an object from the context it was retrieved from. Note though that these methods only detatch the object you pass as an argument, not associated objects, so you'd probably have to walk through the connected objects detatching each one, which is messy.

    var entities1 = new TherapyDatabaseDevEntities();
    var patient1 = entities1.Patients.Single(p => p.Id = 12345);
entities1.Detach(patient1);
//loop through associated entities calling Detach on each

    var entities2 = new TherapyDatabaseDevEntities();
entities2.Attach(patient1);
//loop through associated entities calling Attach on each

My preference would be to use viewmodels so that you aren't editing the entities directly, but representations of them. When a user explicitly saves an object, retrieve that object and update the changes from that object only on a fresh context.

var entities1 = new TherapyDatabaseDevEntities();
var patient1 = entities1.Patients.Single(p => p.Id = 12345);

... dispose of your context, it's no longer needed, and make your changes here

var entities2 = new TherapyDatabaseDevEntities();
var patient2 = entities2.Patients.Single(p => p.Id = 12345);

patient2.Property1 = patient1.Property1;

... update with other changes (there's ways to make this code cleaner, just showing simplest example)

entities2.SaveChanges();
entities2.Dispose();

Some reference material on Attach/Detach here - http://msdn.microsoft.com/en-us/library/bb896271.aspx

Do a search on entity framework context lifetimes, there's a lot of discussion on this subject which might help you decide on a route that suits your needs.

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