如何使用MVC(实体框架)更新主键字段(或主键字段的一部分) [英] How to update primary key field (or Part of primary key field) using MVC (Entity Framework)

查看:187
本文介绍了如何使用MVC(实体框架)更新主键字段(或主键字段的一部分)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表:国家

{

国家varchar(50)

}





样本数据:

国家

1.印度

2 。美国



我想要的是更新国家/地区字段。但是它的 主键 。所以我得到了以下错误。

属性'Country'是对象的关键信息的一部分,无法修改。



所以请帮助我如何完成这项任务。



提前致谢.. 。

推荐答案

你好......



您可以更新任何字段表。为什么你不能在这里编辑主键值的原因是它(主键)已被另一个表作为外键引用。您必须删除/更改其他表中的任何引用行才能更新此国家/地区列。



在附注中,在现实世界中最佳做法是创建一个名为Country的表,其中包含2列。一个整数列,它是名为ID的主键,另一列是名为description的varchar(50)。您可以将ID列设置为自动增量字段以减少麻烦并使描述列成为唯一索引。更新其他表以将ID列称为外键。这种方法的主要优点是,您可以将描述更改国家/地区的名称更改为其他任何内容,我认为您希望实现的目标是什么:)



希望这有帮助



问候,Pasan。
Hi there...

You can update any field in a table. The reason, why you cannot edit the primary key value here, is that it (the primary key) has been referenced by another table as a foreign key. You will have to drop/change any referencing rows in other tables in order to update this "Country" column.

On a side note, in real world the best practice is to create a table called "Country" with 2 columns. One integer column which is the primary key called "ID" and the other column a varchar(50) named "description". You can make the ID column an auto-incremental field to reduce the hassle and make the "Description" column a unique index. Update the other tables to refer to the "ID" column as a foreign key. The key advantage in this approach is, you can change the description which is the name of the country to anything else without updating the ID, which I think what you want to achieve :)

Hope this helps

Regards, Pasan.


此链接可以帮助http://stackoverflow.com/questions/3187963/属性id是对象的一部分 - 关键信息 - 并且不能被修改 [ ^ ]
This link can help http://stackoverflow.com/questions/3187963/the-property-id-is-part-of-the-objects-key-information-and-cannot-be-modified[^]


这篇关于如何使用MVC(实体框架)更新主键字段(或主键字段的一部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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