entity-framework/ef6/modeling/designer/stored-procedures/cud.md
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]");
When mapping the CUD operations to stored procedures, the following considerations apply:
To complete this walkthrough, you will need:
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.
Click Finish. The EF Designer, which provides a design surface for editing your model, is displayed.
Right-click the Person entity type and select Stored Procedure Mapping.
The stored procedure mappings appear in the Mapping Details window.
Click <Select Insert Function>. The field becomes a drop-down list of the stored procedures in the storage model that can be mapped to entity types in the conceptual model. Select InsertPerson from the drop-down list.
Default mappings between stored procedure parameters and entity properties appear. Note that arrows indicate the mapping direction: Property values are supplied to stored procedure parameters.
Click <Add Result Binding>.
Type NewPersonID, the name of the parameter returned by the InsertPerson stored procedure. Make sure not to type leading or trailing spaces.
Press Enter.
By default, NewPersonID is mapped to the entity key PersonID. Note that an arrow indicates the direction of the mapping: The value of the result column is supplied to the property.
Click <Select Update Function> and select UpdatePerson from the resulting drop-down list.
Default mappings between stored procedure parameters and entity properties appear.
Click <Select Delete Function> and select DeletePerson from the resulting drop-down list.
Default mappings between stored procedure parameters and entity properties appear.
The insert, update, and delete operations of the Person entity type are now mapped to stored procedures.
If you want to enable concurrency checking when updating or deleting an entity with stored procedures, use one of the following options:
Open the Program.cs file where the Main method is defined. Add the following code into the Main function.
The code creates a new Person object, then updates the object, and finally deletes the object.
using (var context = new SchoolEntities())
{
var newInstructor = new Person
{
FirstName = "Robyn",
LastName = "Martin",
HireDate = DateTime.Now,
Discriminator = "Instructor"
}
// Add the new object to the context.
context.People.Add(newInstructor);
Console.WriteLine("Added {0} {1} to the context.",
newInstructor.FirstName, newInstructor.LastName);
Console.WriteLine("Before SaveChanges, the PersonID is: {0}",
newInstructor.PersonID);
// SaveChanges will call the InsertPerson sproc.
// The PersonID property will be assigned the value
// returned by the sproc.
context.SaveChanges();
Console.WriteLine("After SaveChanges, the PersonID is: {0}",
newInstructor.PersonID);
// Modify the object and call SaveChanges.
// This time, the UpdatePerson will be called.
newInstructor.FirstName = "Rachel";
context.SaveChanges();
// Remove the object from the context and call SaveChanges.
// The DeletePerson sproc will be called.
context.People.Remove(newInstructor);
context.SaveChanges();
Person deletedInstructor = context.People.
Where(p => p.PersonID == newInstructor.PersonID).
FirstOrDefault();
if (deletedInstructor == null)
Console.WriteLine("A person with PersonID {0} was deleted.",
newInstructor.PersonID);
}
[!NOTE] PersonID is auto-generated by the server, so you will most likely see a different number*
Added Robyn Martin to the context.
Before SaveChanges, the PersonID is: 0
After SaveChanges, the PersonID is: 51
A person with PersonID 51 was deleted.
If you are working with the Ultimate version of Visual Studio, you can use Intellitrace with the debugger to see the SQL statements that get executed.