我使用Entity Framework 4.0. SaveChanges()可能返回0但不会引发异常?例如,添加后.
这是我的代码:
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; }
解决方法
根据
the documentation,DbContext.SaveChanges的返回值为
The number of objects written to the underlying database.
所以你看到的只有在没有实体需要保存到数据库的时候才可能.