Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is SaveChanges() Necessary with Function Imports (Stored Procedures)?

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?
}
like image 758
Didaxis Avatar asked Oct 11 '11 13:10

Didaxis


People also ask

What does the DbContext SaveChanges () method return?

Returns. The number of state entries written to the underlying database. This can include state entries for entities and/or relationships.

When should you call context SaveChanges?

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.

What is SaveChanges method?

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.

Are stored procedures necessary?

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.

What is the use of savechanges () method in SQL Server?

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.

How do I use stored procedures?

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.

How to use stored procedures in EF API?

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.

What are the limitations of a stored procedure?

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.


1 Answers

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).

like image 189
Yakimych Avatar answered Oct 06 '22 01:10

Yakimych