Click here to Skip to main content
15,916,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
device dev = dbEntities.devices.First( i => i.id==1 );
                dev.device_name="newUpdateValue";

                dbEntities.SaveChanges( );


also tried this:

C#
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:

C#
UpdateMethod( DeviceID, DeviceName, ModelNumber );


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.
Posted
Updated 17-Jul-14 23:03pm
v2

1 solution

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[^]
 
Share this answer
 
Comments
Uwakpeter 18-Jul-14 5:42am    
Thanks alot, it works, i spent 2 days trying to figure this out, you have just save me another hours of unfruitful effort. Thanks i really appreciate.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900