如何使用Entity Framework 5更新记录 [英] How Do I Update A Record Using Entity Framework 5
问题描述
我正在使用实体框架工作5数据库第一个工作流程,只是我的第一次尝试,我正在尝试更新记录属性,但它似乎不起作用,错误消息是:
属性'device_name'是对象的关键信息的一部分,无法修改
这里是我的代码:
i am using entity frame work 5 database first workflow, just my first attempt, i am trying to update a record attribute, but it doesn't seem to work, the error message is:
the property 'device_name' is part of the object's key information and cannot be modified
here is my code:
device dev = dbEntities.devices.First( i => i.id==1 );
dev.device_name="newUpdateValue";
dbEntities.SaveChanges( );
也试过这个:
also tried this:
protected int UpdateMethod(int Id, string newValue,string modelNumber)
{
using (var ctx = new GSC_NewSchoolCoreEntities( ) )
{
var query = ( from q in ctx.devices
where q.id == Id
select q ).First( );
query.device_name = newValue;
query.model_number= modelNumber;
int result = ctx.SaveChanges( );
return result;
}
}
通话方式:
the calling method:
UpdateMethod( DeviceID, DeviceName, ModelNumber );
仍然有相同的错误消息,我在网上搜索,看看我是否可以自己修复,但没有一个在线解决方案适合我! device_name和model_number是我想要更新但不是id,请问我有什么遗漏?感谢您的贡献。
still having the same error message, i have search online to see if i could fix this myself, but none of the online solutions work for me! device_name and model_number are what i want to update but not the id, Please is there anything i am missing? Thanks for your contributions.
推荐答案
检查您的表是否在数据库中具有主键列。如果它没有任何主键而不是添加主键。因为如果我们不向表添加主键,那么实体框架会创建自己的密钥集合并在其中添加所有列。
有关更多信息,请查看以下链接
< a href =http://blogershub.com/Archive/2013/10/The-property-is-part-of-the-object-key-information-and-cannot-be-modified#.U8jk_fmSwwk> http: //blogershub.com/Archive/2013/10/The-property-is-part-of-the-object-key-information-and-cannot-be-modified#.U8jk_fmSwwk [ ^ ]
Check whether your table is having primary key column in database or not. If it does not have any primary key than 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.
For more check the following link
http://blogershub.com/Archive/2013/10/The-property-is-part-of-the-object-key-information-and-cannot-be-modified#.U8jk_fmSwwk[^]
这篇关于如何使用Entity Framework 5更新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!