Is SaveChanges() Necessary with Function Imports (

2020-08-22 06:54发布

问题:

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?
}

回答1:

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