Click here to Skip to main content
15,867,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to update a column in my table according to the query. But I get an error. How do I do it.

This hata message: Entity Framework: “Store update, insert, or delete statement affected an unexpected number of rows (0).


I want to DepartmentCode A1 PERSON ID uptade A3.

For example:

Person ID : 1,2,10,55,102 => DeparmentCode:A1

I want to update result: 1,2,10,55,102 => DeparmentCode:A3


TableColumn
PERSON ID NAME SURNAME DEPARTMENT DEPARTMENTCODE

What I have tried:

var ıds=mycontextntities.PERSON.Where(x=>x.DepartmentCode == "A1" && idControl.Where(y=>y==x.PERSONID).Any()).OrderBy(x=>new {x.Name,x.Surname}).ToList();

foreach(var item in ıds)
{
    PERSON pr=new PERSON()
{
      PERSONID=item.PERSONID,
      Name=item.Name,
      Surname=item.Surname,
      Department=item.Department,
      DepartmentCode="A3"
      
};

mycontextntities.Entry(pr).State=EntityState.Modified;
mycontextentities.SaveChanges();

}
Posted
Updated 7-Jan-21 2:48am
v2

1 solution

You could try
C#
// Create a list of ids to update
List<int> idsToUpdate = new List<int>(new int[] { 1, 2, 10, 55, 102 });

// Create an enumeration of corresponding persons
var personsToUpdate = mycontextentities.PERSON.Where
(
   p => idsToUpdate.Contains(p.PERSONID)
);

// Iterate enumerated persons and update their department code
foreach (PERSON p in personsToUpdate)
{
   p.DepartmentCode = "A3";
}

// Update data storage
mycontextentities.SaveChanges();
 
Share this answer
 
Comments
Member 14169626 29-May-20 15:03pm    
Hello id value variable.So var ids table we take the table columns.The list is not fixed as you show.How do I keep the query in the list
phil.o 29-May-20 15:06pm    
Sorry, I don't understand what you mean. Where do ids come from? What is the type of the idControl variable?
Member 14169626 29-May-20 15:21pm    
var ıds quers id give id.You kept it as a list.(List<int> idsToUpdate).Do you want me to keep the query as a list.So those id values ​​aren't that much. There can be more than one id.
phil.o 29-May-20 15:51pm    
Sorry I still don't understand. Please answer my question: what is the type of the idControl variable?
Member 14169626 30-May-20 8:26am    
hello I am getting the above query as ids. And I want to update this query according to the values ​​with the ids value. For example, the ids value gives many values.If it is id I say update.I did it, but despite doing savechanges and updating the value, it does not save it to the database.

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