Compartir a través de


Application Code for Mapped Stored Procedures (Entity Framework)

The application code in this section illustrates how stored procedures that update data are used by the Entity Framework. The object model and stored procedures used in the example are described in Stored Procedure Support (Entity Framework).

When stored procedures are defined in the storage schema and mapped in the mapping specification, they are called implicitly. Code sequences used to create, update, and delete entities are the same whether or not you are using stored procedures in the data model.

Creating an Entity using a Stored Procedure

The following example creates a new SalesOrderDetail entity and inserts it in storage.

    SalesOrderDetail newSODetail = new SalesOrderDetail();

    newSODetail.CarrierTrackingNumber = "4E0A-4F89-AE";
    newSODetail.ModifiedDate = DateTime.Now;
    newSODetail.SpecialOfferID = 1;
    newSODetail.rowguid = Guid.NewGuid(); 
    newSODetail.UnitPrice = 45.00M;
    newSODetail.OrderQty = 5;
    newSODetail.ProductID = 680;

    objCtx.AddToSalesOrderDetails(newSODetail);
    ObjectParameter par = new ObjectParameter("p", 45678);
    SalesOrderHeader soHeader = 
                  objCtx.SalesOrderHeader.Where("it.SalesOrderID = @p", par).First<SalesOrderHeader>();
    soHeader.SalesOrderDetail.Add(newSODetail);
    objCtx.SaveChanges();

Modifying an Entity using a Stored Procedure

The following code modifies the order quantity property of a SalesOrderDetail entity.

    ObjectParameter par2 = new ObjectParameter("p", 45678);
     SalesOrderHeader soHeader2 = 
                      objCtx.SalesOrderHeader.Where("it.SalesOrderID = @p", par2).First<SalesOrderHeader>();
     soHeader2.SalesOrderDetail.Load();
     foreach (SalesOrderDetail sodet in soHeader2.SalesOrderDetail)
     {
            Console.WriteLine(sodet.SalesOrderDetailID + " Prod: " +
            sodet.ProductID + " Qty: " + sodet.OrderQty);
            if (sodet.ProductID.Equals(680))
            {
                  sodet.OrderQty = 6;
                  objCtx.SaveChanges();
                  break; 
            }
      }

Deleting an Entity using a Stored Procedure

The following code deletes a SalesOrderDetail entity using a stored procedure.

    ObjectParameter par2 = new ObjectParameter("p", 45678);
    SalesOrderHeader soHeader2 = 
    objCtx.SalesOrderHeader.Where("it.SalesOrderID = @p",
                                      par2).First<SalesOrderHeader>();
    soHeader2.SalesOrderDetail.Load();
    foreach (SalesOrderDetail sodet in soHeader2.SalesOrderDetail)
    {
                Console.WriteLine(sodet.SalesOrderDetailID + 
                  " Prod: " + sodet.ProductID + " Qty: " +
                  sodet.OrderQty);
            if (sodet.ProductID.Equals(680))
            {
                  objCtx.DeleteObject(sodet);
                  objCtx.SaveChanges();
                  break; 
            }

    }

Complete Application Code

All the examples in this section are included in the following example. Comment out the block of code that creates a new SalesOrderDetail entity after it has run once.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AdventureWorksModel;
using System.Data.Objects;

namespace AdvWksSalesSProcsClient
{
    class Program
    {
        static void Main(string[] args)
        {
            using (AdventureWorksEntities objCtx = new AdventureWorksEntities())
            {
                SalesOrderDetail newSODetail = new SalesOrderDetail();

                newSODetail.CarrierTrackingNumber = "4E0A-4F89-AE";
                newSODetail.ModifiedDate = DateTime.Now;
                newSODetail.SpecialOfferID = 1;
                newSODetail.rowguid = Guid.NewGuid(); 
                newSODetail.UnitPrice = 45.00M;
                newSODetail.OrderQty = 5;
                newSODetail.ProductID = 680;

                objCtx.AddToSalesOrderDetails(newSODetail);
                
                ObjectParameter par = new ObjectParameter("p", 45678);
                SalesOrderHeader soHeader = 
                      objCtx.SalesOrderHeader.Where("it.SalesOrderID = @p",
                                                     par).First<SalesOrderHeader>();

                soHeader.SalesOrderDetail.Add(newSODetail);
                objCtx.SaveChanges();

                ObjectParameter par2 = new ObjectParameter("p", 45678);
                SalesOrderHeader soHeader2 = 
                      objCtx.SalesOrderHeader.Where("it.SalesOrderID = @p",
                                                      par2).First<SalesOrderHeader>();
                soHeader2.SalesOrderDetail.Load();
                foreach (SalesOrderDetail sodet in soHeader2.SalesOrderDetail)
                {
                    Console.WriteLine(sodet.SalesOrderDetailID + 
                              " Prod: " + sodet.ProductID + " Qty: " + sodet.OrderQty);
                    if (sodet.ProductID.Equals(680))
                    {
                        objCtx.DeleteObject(sodet);
                        objCtx.SaveChanges();
                        break; 
                        
                    }

                }

                foreach (SalesOrderDetail sodet in soHeader2.SalesOrderDetail)
                {
                    Console.WriteLine(sodet.SalesOrderDetailID + 
                       " Prod: " + sodet.ProductID + 
                       " Qty: " + sodet.OrderQty);
                    if (sodet.ProductID.Equals(680))
                    {
                        sodet.OrderQty = 6;
                        objCtx.SaveChanges();
                        break;
                    }

                }

            }
        }
    }
}

See Also

Tasks

How to: Execute a Query Using a Stored Procedure (Entity Framework)
How to: Define a Model with a Stored Procedure (Entity Framework)

Concepts

Stored Procedure Support (Entity Framework)
AdventureWorks Sales Model (EDM)
Mapping Association Sets to Stored Procedures (Entity Framework)
ModificationFunctionMapping (EntityTypeMapping)
ModificationFunctionMapping (AssociationSetMapping)