Export (0) Print
Expand All

Application Code for Mapped Stored Procedures (Entity Framework)

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

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

Community Additions

Show:
© 2014 Microsoft