You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I have a little question. You have the Save method in the FoodSqlRepository class. However, this method always return positive number from 0, because the returned number is the number of state entries written to the database.
In which cases can the returned number be negative?
publicclassFoodSqlRepository:IFoodRepository{// your codepublicboolSave(){return(_foodDbContext.SaveChanges() >= 0);}}
The text was updated successfully, but these errors were encountered:
Thank you for your question. In the context of Entity Framework (or most ORMs), the SaveChanges() method returns the number of state entries written to the database. This number will always be zero or positive, as it represents the count of affected rows.
The method signature for SaveChanges() is:
public int SaveChanges();
It returns an integer indicating how many entities were updated in the database.
A return value of 0 means no changes were made.
A positive return value means that number of changes were successfully saved.
It is not possible for SaveChanges() to return a negative number, as it would not make sense in the context of counting affected rows.
Regarding the Save method in the FoodSqlRepository class, it is checking whether the return value of SaveChanges() is greater than or equal to 0, which will always be true because SaveChanges() cannot return a negative number. So the method as currently implemented will always return true as long as SaveChanges() doesn't throw an exception.
Your observation is correct. If you are looking to check for a successful save operation, the method's current implementation already ensures this by verifying that the number of state entries written to the database is non-negative.
If you have further questions or need additional clarifications, feel free to ask!
Hi, I have a little question. You have the Save method in the FoodSqlRepository class. However, this method always return positive number from 0, because the returned number is the number of state entries written to the database.
In which cases can the returned number be negative?
The text was updated successfully, but these errors were encountered: