Dear I have a table in which I have made a primary key as combination of three columns like (ID,Student_branch,School_branch)
Now I am getting all data from the table by giving a student_ID parameter and i get one row which is fine now I want to add only school Branch column when I call this
studnet.School_branch= "ABC";
db.Entry(School_branch).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
I get this error The property 'School_branch' is part of the object's key information and cannot be modified.
Prasad RaveendranPosted Mar 3, 2024, 2:05 AM
Hello Pakeeza,
Sorry to hear that :(
Here are a few things you can check and try:
Ensure the Key Attributes are Correct: Make sure that the
ID,Student_branch, andSchool_branchattributes in your Entity class match the actual key attributes in your database. Verify that they are marked as[Key]in the class definition.2. Ensure No Additional Constraints: Check if there are any database triggers, constraints, or other rules that might be preventing the update.
3. Try Loading and Updating Without Querying First: Instead of querying and then modifying, try creating a new instance of your entity, setting the necessary properties, and then updating it:
4. Check Database Logs: If possible, check the database logs or exceptions to get more specific information about why the update is failing.
If the issue persists after checking these points, there might be some specific details about your database model or configuration that require a closer look. If you can provide more details or code snippets related to your database model and entity class, I might be able to provide more targeted assistance.
PakeezaPosted Mar 1, 2024, 6:49 PM
Hi Prasad what you sid I am exactly doing the same but the column I am going to update is a part of primary key so getting exception "The property 'MyColumntobeupdated' is part of the object's key information and cannot be modified. "
I also try to update all three columns whichmakes the primary key but same error
Prasad RaveendranPosted Feb 26, 2024, 2:00 AM
When you define a composite primary key using multiple columns (in your case, ID, Student_branch, School_branch), Entity Framework treats those columns as a single unit, and you cannot modify individual columns of a primary key directly.
If you need to update the
School_branchvalue for a specific row, you should update the entire entity instead of just modifying theSchool_branchproperty. Here is an example of how you can achieve this:Replace
YourTableNamewith the actual name of your table and adjust the condition in theWhereclause based on your requirements.By updating the entire entity and marking it as modified, Entity Framework will handle updating all the properties, including those that are part of the composite primary key.
Sam HobbsPosted Feb 25, 2024, 9:12 PM
I think you must delete the record then add it again using the new data. I am not an expert but probably you can make a transaction that does all that and then data would not be lost if there is an error.