what does public int SaveChanges(); return in EF4?

curiouswebster
curiouswebster used Ask the Experts™
on
I see my entity has this method, which I have used successfully.  But I am not yet checking the returned value.

Does anyone know what's returned on success? How about on failure?

Also, if TryUpdateModel(myModel) succeeds, is there any chancce of failure?

Thanks,
newbieweb
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Returns the number of objects that were in an Added, Modified, or Deleted state.  Basically, the number of row affected. (http://msdn.microsoft.com/en-us/library/bb336792.aspx)

StoreChanges throws an exception if the transaction failed, the return value doesn't indicate failure (it isn't a code or anything).

As for TryUpdateModel (MVC), according to the docs (http://msdn.microsoft.com/en-us/library/dd460189.aspx) it can throw an ArgumentNullException if the ValueProvider is null.  It will not, however, throw an InvalidOperationException if the model state is invalid.
curiouswebsterSoftware Engineer

Author

Commented:
So it seems there's no reason for me to inspect the returned value other than having one more text to verify success.  By that I mean if I updated 1 record, the return vaue should be 1.  If not, throw my own exception.

Is that logical?
Commented:
I cannot think of any occasion where you would need to check this, but if you wanted to you could.  For a complicated model you would probably have a hard time determining how many objects were modified before hand.  Probably not worth the effort.  None of the cample code I see do anything with the return value.

If there were a failure it would throw an exception.  See http://msdn.microsoft.com/en-us/library/bb738618.aspx for more information about this.
curiouswebsterSoftware Engineer

Author

Commented:
Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial