当列之一是PrimeryKey也是FK时,如何更新表列. [英] How to Update Table Column when one of column is PrimeryKey and also FK.

查看:88
本文介绍了当列之一是PrimeryKey也是FK时,如何更新表列.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试更新其时,显示此错误.
定义对象身份的成员不能更改.请考虑添加具有新身份的新对象,然后删除现有的对象."

任何解决方案.如何更新PK和FK列.

谢谢

When I try to Update its show this error.
"A member defining the identity of the object cannot be changed. Consider adding a new object with new identity and deleting the existing one instead."

Any solution for this. How to update PK & FK column.

Thanks

推荐答案

您为什么要尝试更新主键?拥有既充当主键又充当外键的列是一个奇怪的选择吗?我建议您需要将这些字段分开,以便拥有独立的FK和PK,然后在更新记录时可以更新外键而不影响PK.

这样说,您可能有一个我看不见的原因(假设我知道您为什么要尝试做某事,这就是我的自大程度),因此,如果您可以进一步解释它们为什么相同的原因领域,我们也许可以提出其他建议.



我很遗憾地说,但是您的设计有一个根本性的问题-您确实不应该对两者使用相同的字段.最好的选择是考虑将FK与PK分开.

如果由于某种原因不能执行此操作,则必须使用存储过程来执行更新.在存储过程中,您需要使用
Why are you trying to update the primary key? Having a column that acts as both primary key and foreign key is a strange choice? I would suggest that you need to separate these fields out so that you have a separate FK and PK, then when you update your record you can update the foreign key without impacting the PK.

Saying that, you may have a reason that I can''t see (and it would be the height of arrogance of me to assume I knew why you were trying to do something), so if you could explain further why they are the same field, we may be able to suggest alternatives.



I''m sorry to say, but your design has a fundamental problem - you really shouldn''t use the same field for both. The best option would be to consider splitting the FK apart from the PK.

If you can''t do that for some reason, you are going to have to use a stored procedure to perform the update. In your stored procedure, you need to disable the constraints using
ALTER TABLE tablename WITH NOCHECK CONSTRAINT ALL

更新您的PK,然后重新启用约束.

Update your PK and then reenable your constraints.


理想情况下,应选择主键或外键,这样就永远不需要进行更新.
Ideally, a primary or foreign key should be chosen so that it never need be updated.


这篇关于当列之一是PrimeryKey也是FK时,如何更新表列.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆