属性“text”是对象关键信息的一部分,无法修改
所以我在数据库中有一个表,其中有一列只是 nvarchar(800)。
当我尝试这样做时:
try
{
UserTable = (from x in entities.userTable where x.uID == uID select x).Single();
UserTable.DateCreated = DateTime.Now;
UserTable.text= newText;
Update(UserTable);
}
我在 catch 中遇到异常:“属性‘文本’是对象关键信息的一部分,无法修改。”
当我查看表时,我不知道查看“key”或“index”下的任何内容。所以这不是关键,我不明白为什么 C# 给我错误的信息。 SQL Management Studio 中没有任何内容说明“文本”是键或索引。我该怎么办?
So I have a table in the database, with a column that is simply an nvarchar(800).
When I try to do:
try
{
UserTable = (from x in entities.userTable where x.uID == uID select x).Single();
UserTable.DateCreated = DateTime.Now;
UserTable.text= newText;
Update(UserTable);
}
I get the exception in catch: "The property 'text' is part of the object's key information and cannot be modified."
When I look into the table, I don't see anything under "key" or "index". So it's not a key, I don't understand why C# is giving me incorrect information. Nothing in SQL Management Studio says anything about "text" being a key or index. What do I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
桌上还有PK吗?如果不是,EF 将使用所有字段/列作为“关键信息”的一部分。
Is there a PK on the table at all? If not, EF uses all of the fields/columns as part of the "key information."
双击 Models 文件夹下的 edmx 文件来查看数据模型。
确保按键映射正确。如果右键单击某列,则可以切换实体键属性。一旦相关列未标记为实体键,您应该能够更新该值。
View the data model by double clicking on the edmx file under the Models folder.
Make sure the keys are properly mapped. If you right click on a column you can toggle the Entity Key property. Once the column in question is not marked as entity key you should be able to update the value.
按照以下简单步骤操作
步骤 1:检查您的表在数据库中是否有主键列。如果没有主键,则添加主键。因为如果我们不向表添加主键,实体框架就会创建自己的键集合并在其中添加所有列。
步骤 2:打开 .edmx 文件以查看表对象映射。您将能够看到表的每一列都有类似主键的图标。因此,请点击您的页面并从数据库更新 .edmx。
步骤 3:如果您仍然在所有列上看到相同的主键图标,则单击要一一更新的列名称,然后转到属性窗口并将 Entity Key 属性设置为 false。
Follow these simple steps
Step 1: Check whether your table is having primary key column in database or not. If it does not have any primary key then add a primary key. Because if we don't add a primary key to table than entity framework creates its own key collection and add all columns in it.
Step 2: Open your .edmx file to see table object mapping. You will be able to see each column of table is having icon like primary key. So Write click on your page and update .edmx from database.
Step 3: If you still see same primary key icons on all columns than click on column name you want to update one by one and go to property window and Set Entity Key property to false.
这里的问题是实体框架无法更改特定表的主键。现在,如果您没有为表指定主键,实体框架会将所有内容视为主键,因此您将无法对表进行更改。您需要做的是,在表中定义主键,从模型浏览器中删除模型中的表,然后在模型中重新添加表。这肯定能解决它。快乐编码。干杯!
The problem here is that Entity Framework cannot make changes to the primary key of the particular table. Now if you have not specified a primary key for your Table, entity framework will consider everything as the primary key, and thus you won't be able to make changes to the table. What you need to do is, define a primary key in the table, delete the table from your model from the Model Browser, and then re-add the table in the model. This shall surely fix it. Happy coding. Cheers!