Edit

Designer CUD Stored Procedures

This step-by-step walkthrough show how to map the create\insert, update, and delete (CUD) operations of an entity type to stored procedures using the Entity Framework Designer (EF Designer).  By default, the Entity Framework automatically generates the SQL statements for the CUD operations, but you can also map stored procedures to these operations.  

Note, that Code First does not support mapping to stored procedures or functions. However, you can call stored procedures or functions by using the System.Data.Entity.DbSet.SqlQuery method. For example:

var query = context.Products.SqlQuery("EXECUTE [dbo].[GetAllProducts]");

Considerations when Mapping the CUD Operations to Stored Procedures

When mapping the CUD operations to stored procedures, the following considerations apply:

  • If you are mapping one of the CUD operations to a stored procedure, map all of them. If you do not map all three, the unmapped operations will fail if executed and an UpdateException will be thrown.
  • You must map every parameter of the stored procedure to entity properties.
  • If the server generates the primary key value for the inserted row, you must map this value back to the entity's key property. In the example that follows, the InsertPerson stored procedure returns the newly created primary key as part of the stored procedure's result set. The primary key is mapped to the entity key (PersonID) using the  feature of the EF Designer.
  • The stored procedure calls are mapped 1:1 with the entities in the conceptual model. For example, if you implement an inheritance hierarchy in your conceptual model and then map the CUD stored procedures for the Parent (base) and the Child (derived) entities, saving the Child changes will only call the Child’s stored procedures, it will not trigger the Parent’s stored procedures calls.

Prerequisites

To complete this walkthrough, you will need:

Set up the Project

  • Open Visual Studio 2012.
  • Select File-> New -> Project
  • In the left pane, click Visual C#, and then select the Console template.
  • Enter CUDSProcsSample as the name.
  • Select OK.

Create a Model

  • Right-click the project name in Solution Explorer, and select Add -> New Item.

  • Select Data from the left menu and then select ADO.NET Entity Data Model in the Templates pane.

  • Enter CUDSProcs.edmx for the file name, and then click Add.

  • In the Choose Model Contents dialog box, select Generate from database, and then click Next.

  • Click New Connection. In the Connection Properties dialog box, enter the server name (for example, (localdb)\mssqllocaldb), select the authentication method, type School for the database name, and then click OK. The Choose Your Data Connection dialog box is updated with your database connection setting.

  • In the Choose Your Database Objects dialog box, under the Tables node, select the Person table.

  • Also, select the following stored procedures under the Stored Procedures and Functions node: DeletePerson, InsertPerson, and UpdatePerson.

  • Starting with Visual Studio 2012 the EF Designer supports bulk import of stored procedures. The Import selected stored procedures and functions into the entity model is checked by default. Since in this example we have stored procedures that insert, update, and delete entity types, we do not want to import them and will uncheck this checkbox.

    Import S Procs

  • Click Finish. The EF Designer, which provides a design surface for editing your model, is displayed.

Map the Person Entity to Stored Procedures

  • Right-click the Person entity type and select Stored Procedure Mapping.

  • The stored procedure mappings appear in the Mapping Details window.

  • Click  and select UpdatePerson from the resulting drop-down list.

  • Default mappings between stored procedure parameters and entity properties appear.

  • Click