处理 EF 4 中的循环引用
我有一个遗留数据库设计,我正在尝试使用 EF 4 来解决它。本质上,我有两个相互引用的表,在尝试添加新条目时会导致问题。
我的结构是这样的:
CREATE TABLE [dbo].[Account] (
[AccountId] INT IDENTITY (1, 1) NOT NULL,
[PrimaryPersonId] INT NULL,
[other columns])
CREATE TABLE [dbo].[Person] (
[PersonId] INT IDENTITY (1, 1) NOT NULL,
[AccountId] INT NOT NULL,
[other columns])
Person 有一个 Account (AccountId) 的外键,Account 有一个 Person (PrimaryPersonId) 的外键。当创建新的帐户和人员时,这显然是一个问题。目前的解决方案是在 Person 表上使用插入触发器,该触发器在创建人员时使用新的 PrimaryPersonId 更新 Account 表。
我希望摆脱对触发器的需求,并在可能的情况下将此代码带入模型中,这样就不会发生“神奇”的情况。有没有好的方法可以使用 EF 4 来做到这一点?
I have a legacy database design that I'm trying to work around with EF 4. Essentially, I have two tables that reference each other causing issues when trying to add new entries.
My structure is this:
CREATE TABLE [dbo].[Account] (
[AccountId] INT IDENTITY (1, 1) NOT NULL,
[PrimaryPersonId] INT NULL,
[other columns])
CREATE TABLE [dbo].[Person] (
[PersonId] INT IDENTITY (1, 1) NOT NULL,
[AccountId] INT NOT NULL,
[other columns])
Person has a foreign key to Account (AccountId) and Account has a foreign key to Person (PrimaryPersonId). When creating a new account and person, this is obviously a problem. Currently the solution is to use an insert trigger on the Person table that updates the Account table with a new PrimaryPersonId when a person is created.
I'd like to get away from needing triggers and bring this code into the model if possible so there's less "magic" happening. Is there a good way to do this with EF 4?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议改为在代码中进行插入。构建一个接受人员对象作为参数的帐户方法,以将人员对象添加到帐户表中。您甚至可以做得更大,在任何处理插入到该表中的东西上包装一个接口,这样您就可以确保它总是发生。
布莱克·罗杰斯
Onyxtek 软件解决方案有限责任公司
I would suggest moving to having the insert happen in code. Build out an account method that accepts a person object as a parameter) to add the person object into the account table. You could even go bigger and wrap an interface on anything that handles inserting into that table so that you can make sure it ALWAYS happens.
Blake Rogers
Onyxtek Software Solutions, LLC