映射的存储过程的应用程序代码(实体框架)

本节中的应用程序代码演示实体框架如何使用更新数据的存储过程。本示例中使用的对象模型和存储过程在存储过程支持(实体框架) 中进行介绍。

如果在存储架构中定义并以映射规范映射存储过程,将隐式调用这些存储过程。无论是否使用数据模型中的存储过程,用于创建、更新和删除实体的代码序列都是相同的。

使用存储过程创建实体

下面的示例创建一个新的 SalesOrderDetail 实体并将它插入到存储中。

    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();

使用存储过程修改实体

下面的代码修改 SalesOrderDetail 实体的订单数量属性。

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

使用存储过程删除实体

下面的代码使用存储过程删除 SalesOrderDetail 实体。

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

    }

完整的应用程序代码

本节中的所有示例都包含在下面的示例中。在创建新 SalesOrderDetail 实体的代码块运行一次之后,请将该代码块注释掉。

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

                }

            }
        }
    }
}

另请参见

任务

如何:使用存储过程执行查询(实体框架)
如何:使用存储过程定义模型(实体框架)

概念

存储过程支持(实体框架)
AdventureWorks 销售模型 (EDM)
将关联集映射到存储过程(实体框架)
ModificationFunctionMapping (EntityTypeMapping)
ModificationFunctionMapping (AssociationSetMapping)