实体框架 4.1:可以从包含多个表的数据上下文中保存单个表吗?
我有一个先有鸡还是先有蛋的问题,这很简单,所以我想我会问保存聚合根的正常模式是什么,其中所有主键都是身份字段。
我有一个典型的联系人实体:
Customer {
HomeAddress {
}
WorkAddress {
}
}
两个地址都存储在地址表中,唯一的主键是身份字段。我们相互检查所有字段以保留唯一的地址记录。
问题是:
- 我需要检索地址标识字段来连接外键,因此仅当地址记录唯一时,我才会在保存客户记录之前保存地址记录,否则我会加载现有地址。
- 如果地址与客户位于同一个 DC 中,则客户保存得太早(并非所有记录都已设置)。
- 如果地址位于单独的 DC 中,则它不会连接到拥有自己的 DC 的客户记录,因为您无法拥有与两个 DC 关联的实体(无法在一个 DC 中打开,然后保存在另一个 DC 中。)
所以我的想法是,我需要为每个地址建立一个单独的存储库,然后在另一个 DC 中再次单独加载该地址,从而对数据库中的相同信息。
有没有办法将记录部分保存在实体框架 4.1 的数据上下文/容器中?例如,在仍处于同一 DC 中的情况下自行保存地址?
I have a chicken and egg problem, it's trivial, so I thought I would ask what's the normal pattern to save an aggregate root where all the primary keys are identity fields.
I have a typical contact entity:
Customer {
HomeAddress {
}
WorkAddress {
}
}
where both addresses are stored in the address table and the only primary key is the identity field. We check all fields against each other to keep unique address records.
Here's the problem:
- I need to retrieve the Address identity field to hookup the foreign keys, so I save the Address record prior to saving the Customer record only if it's unique, otherwise I load that existing Address.
- If Address is in the same DC as Customer, then customer saves too prematurely (not all records are set.)
- If Address is in a separate DC, then it doesn't hookup to the Customer record that has it's own DC because you cannot have an entity associated with two DCs (can't open in one, then save in another.)
So my thinking is that I would need a separate repository for every Address, then separately load the address again in the other DC, making redundant calls to the database for the same information.
Is there a way to partially save records in a data context / container in Entity Framework 4.1? For example, to save Address by itself while still being in the same DC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,你大胆问题的答案是“否”。上下文是一个工作单元,
SaveChanges
在单个事务中将每个新的、更改的或删除的对象提交到数据库。您不能选择性地说:仅保存这个或那个对象,或者仅保存状态为Added
的实体,并且不提交状态为Modified
或Deleted
的实体或其他什么。作为解决方法,您可以尝试:
这样
address
永远不会同时附加到两个上下文。我不确定这是否有效。编辑
我必须添加(因为我上面的代码看起来很奇怪)“通常”你可以单独在
context1
中完成所有这些操作。但我这样理解你的第 2 点,即在SaveChanges
之前// ...
(我不明白)发生了一些事情,这会阻止你保存新的内容地址和客户同时。The answer to your bold question is "No" as far as I can tell. The context is a unit of work and
SaveChanges
commits every new, changed or deleted object to the database in a single transaction. You cannot selectively say: Save only this or that object or save only entities in stateAdded
and don't commit entities in stateModified
orDeleted
or something.As a workaround you could try that:
This way
address
is never attached to the two contexts at the same time. I am not sure though if this works.Edit
I must add (because my code above looks so weird) that "normally" you could do all this in
context1
alone. But I understood your point 2 this way that there is something happening in// ...
(which I don't understand) beforeSaveChanges
which prevents you to save the new address and the customer at the same time.