Is SaveChanges() necessary with function imports (stored procedures)?
Example:
void foo(Product product)
{
// AddProduct is a function import of a stored procedure
entities.AddProduct(product.Name, product.Price, product.Description);
entities.SaveChanges(); // Is this necessary?
}
Returns. The number of state entries written to the underlying database. This can include state entries for entities and/or relationships.
If you need to enter all rows in one transaction, call it after all of AddToClassName class. If rows can be entered independently, save changes after every row.
In Entity Framework, the SaveChanges() method internally creates a transaction and wraps all INSERT, UPDATE and DELETE operations under it. Multiple SaveChanges() calls, create separate transactions, perform CRUD operations and then commit each transaction.
A stored procedure provides an important layer of security between the user interface and the database. It supports security through data access controls because end users may enter or change data, but do not write procedures.
SaveChanges (Boolean) Saves all changes made in this context to the database. This method will automatically call DetectChanges () to discover any changes to entity instances before saving to the underlying database. This can be disabled via AutoDetectChangesEnabled . SaveChanges () Saves all changes made in this context to the database.
You may want to override these steps and use your own predefined stored procedures. You can use stored procedures either to get the data or to add/update/delete the records for one or multiple database tables.
You can use stored procedures either to get the data or to add/update/delete the records for one or multiple database tables. EF API creates a function instead of an entity in EDM for each stored procedure and User-Defined Function (UDF) in the target database. Let's use stored procedure to fetch the data from the database.
This means that a stored procedure must return all the columns of the corresponding table of an entity. Result cannot contain related data. This means that a stored procedure cannot perform JOINs to formulate the result.
According to MSDN, SaveChanges
Persists all updates to the data source and resets change tracking in the object context.
That is, for any entities that are attached to the context and which you have added, modified or deleted, EF will generate the corresponding SQL code and run it against the database. In your case you are already running SQL code (more or less) directly against the database by calling the AddProduct
stored procedure. So in your case SaveChanges
won't do anything and is not necessary (unless you have other unsaved changes on the ObjectContext of course).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With