开发者

The property 'text' is part of the object's key information and cannot be modified

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?


Is there a PK on the table at all? If not, EF uses all of the fields/columns as part of the "key information."


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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜