user2998990
user2998990

Reputation: 980

context.SaveChanges return 0 when updated without any change

context.SaveChanges() returns 0 if I only hit the update button. If I don't change anything and just hit the update button, it returns 0. I am checking on the value which is returned by SaveChanges. Which are the conditions where SaveChanges returns 0. What are the return values indicate?

Following is my code.

int returnValue = CS.SaveChanges();
                   return returnValue == 1 ? "User profile has been updated successfully" : "Unable to update";

Upvotes: 7

Views: 38276

Answers (1)

Marco
Marco

Reputation: 23937

According to the documentation the return value is the number of objects updated in the context:

Return Value
Type: System.Int32
The number of objects written to the underlying database.

So your method could look like this:

int returnValue = CS.SaveChanges();
return returnValue > 0 ? 
    String.Format("{0} User profiles have been updated successfully.", returnvalue) : 
    "No updates have been written to the database.";

Upvotes: 18

Related Questions