Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is that possible, DbContext.SaveChanges() returns 0 but doesn't have an exception?

Tags:

I use Entity Framework 4.0. Is it possible that SaveChanges() returns 0 but doesn't throw an exception? For example, after adding.

Here is my code:

try {     _context.CodeProducts.Add(entity);     _context.SaveChanges();      //Shell I control return result from SaveChanges() in here.     //However doesn't throw an exceoption?      return new MethodResponse()     {         ResultText = "Successful",         Type = MethodResponse.ResponseType.Succeed     }; } catch (OptimisticConcurrencyException exc) {     throw exc; } catch (UpdateException exc) {     throw exc; } catch (Exception exc) {     throw exc; } 
like image 827
cagin Avatar asked Jun 19 '12 08:06

cagin


1 Answers

According to the documentation, the return value of DbContext.SaveChanges is

The number of objects written to the underlying database.

So what you see is only possible, when no entities needed to be saved to the database.

like image 61
Klaus Byskov Pedersen Avatar answered Oct 17 '22 06:10

Klaus Byskov Pedersen