Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following code has 9 records, finds 9 records. Savechanges as it updates the record it found but it does not update Where is the error how can I update.



There are 9 records. I want to update the gender part of these records.

Table column:
PersonNo,Name,Surname,Gender,Birthdate

What I have tried:

var personelUpdate = entities.person.AsNoTracking().Where(x => x.Gender == "Male").
                ToList();


            if (personUpdate.Count() != 0)
            {
                foreach (var item in personUpdate)
                {
                    item.Gender = "NOT KNOW";
                    entities.SaveChanges();
                }
            }
Posted
Updated 31-May-20 8:22am
v2

1 solution

Try it without the "AsNoTracking" as the entities you are getting in personUpdate are probably not having their changes reflected in the db context.
 
Share this answer
 
Comments
Member 14169626 31-May-20 14:36pm    
If I uninstall asnotracking i get this error "An unhandled exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in EntityFramework.dll Additional information: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."
F-ES Sitecore 31-May-20 15:06pm    
You need to work out what is failing validation and why and solve that issue. Look for validation attributes on the properties of the underlying class.
Member 14169626 31-May-20 15:13pm    
Thanks a lot.
The solution could not be used because the error table character is 1. It was without asnotracking

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