2

Hi i am trying to update a record from the database, but for some reason the record is not updated. I am using ADO.NET entity Framework 4.0

public void updateCredits(string username)
 {
     myDBEntities Entity = new myDBEntities();
     User u = Entity.Users.SingleOrDefault(u => u.username == username)
     u.firstname = "name";
     u.credits = 11.5;
     Entity.SaveChanges();
 }

I tried restarting VS and even the SQL Server but no luck. Am i doing something wrong?

3
  • What's the type of u.credits Commented Apr 13, 2013 at 18:27
  • There's nothing wrong with your code -- there's obviously more to this issue than the actual code you've displayed. Commented Apr 13, 2013 at 18:28
  • Sure change tracking is enabled? Commented Apr 13, 2013 at 18:32

1 Answer 1

1

It looks like there is an issue with change tracking. You can try manually marking the User entity as changed like so:

public void updateCredits(string username)
 {
     myDBEntities Entity = new myDBEntities();
     User u = Entity.Users.SingleOrDefault(u => u.username == username)
     u.firstname = "name";
     u.credits = 11.5;
     Entity.Entry(u).State = EntityState.Modified;  //<-- manually indicate the entity was changed
     Entity.SaveChanges();
 }

If the above code works, then there's a good chance ChangeTracking is being disabled somehow.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you brian for your fast reply. I managed to solve the problem by removing all the tables from the ADO.NET Entity and adding them again. :) Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.