context.SaveChanges return 0 when updated without any change

entity-framework

Question

context.SaveChanges returns 0 if i only hit the update button. If i dont change anything and just hit the update button, it return me 0. I am checking on the value which is returned by savechanges. Which are the conditions where savechanges returns me 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";
1
2
1/16/2015 6:12:32 AM

Accepted Answer

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.";
6
1/12/2017 7:15:18 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow