How to: Add and Modify Objects with Modification Stored Procedures (Entity Framework)

The Entity Framework enables you to specify stored procedures to be used when modifying entity data. These stored procedures replace the methods generated by the Entity Framework. Stored procedures are called implicitly, so no changes are required to the data model defined in the conceptual schema or existing application code. For more information, see Stored Procedure Support (Entity Framework).

The examples in this topic are based on the Adventure Works Sales Model. To run the code in this example, you must have already completed the steps in How to: Define a Model with Modification Stored Procedures (Entity Framework) to define the modification stored procedures for the SalesOrderDetail entity type.

Example

The following example creates a new SalesOrderDetail object. This new item is inserted in the database when SaveChanges is called.

        Using objCtx As AdventureWorksEntities = _
                                  New AdventureWorksEntities()

            Dim newSODetail As SalesOrderDetail = _
                                  New SalesOrderDetail()

            newSODetail.CarrierTrackingNumber = "4E0A-4F89-AE"
            newSODetail.ModifiedDate = DateTime.Now
            newSODetail.SpecialOfferID = 1
            newSODetail.rowguid = Guid.NewGuid()
            newSODetail.UnitPrice = CDec(45.0)
            newSODetail.OrderQty = 5
            newSODetail.ProductID = 680
            objCtx.AddToSalesOrderDetail(newSODetail)
            Dim par As ObjectParameter = _ 
                        New ObjectParameter("p", 45678)
            Dim soHeader As SalesOrderHeader
            soHeader = objCtx.SalesOrderHeader.Where( _
                     "it.SalesOrderID = @p", par).FirstOrDefault()
            soHeader.SalesOrderDetail.Add(newSODetail)
            objCtx.SaveChanges()

        End Using
    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.AddToSalesOrderDetail(newSODetail);
    ObjectParameter par = new ObjectParameter("p", 45678);
    SalesOrderHeader soHeader = 
         objCtx.SalesOrderHeader.Where("it.SalesOrderID = @p",
         par).First<SalesOrderHeader>();
    soHeader.SalesOrderDetail.Add(newSODetail);
    objCtx.SaveChanges();

The following code modifies the order quantity property of a SalesOrderDetail object. This change is persisted to the database when SaveChanges is called.

        Using objCtx As AdventureWorksEntities = _
                                  New AdventureWorksEntities()
            Dim par2 As ObjectParameter = _
                               New ObjectParameter("p", 45678)
            Dim soHeader2 = _
                      objCtx.SalesOrderHeader.Where( _
                      "it.SalesOrderID = @p", par2).FirstOrDefault()
            soHeader2.SalesOrderDetail.Load()

            For Each sodet As SalesOrderDetail _
                           In soHeader2.SalesOrderDetail
                Console.WriteLine("
            Next
        End Using
     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; 
            }
      }

The following example deletes a SalesOrderDetail object. This row is deleted from the database when SaveChanges is called.

            Dim par2 As ObjectParameter = _
                        New ObjectParameter("p", 45678)
            Dim soHeader2 = _
                      objCtx.SalesOrderHeader.Where( _
                      "it.SalesOrderID = @p", par2).FirstOrDefault()
            soHeader2.SalesOrderDetail.Load()

            For Each sodet As SalesOrderDetail In soHeader2.SalesOrderDetail
                Console.WriteLine("ID: {0} Prod: {1} Qty: {2}", _
                                  sodet.SalesOrderDetailID, _
                                  sodet.ProductID, sodet.OrderQty)
                If (sodet.ProductID.Equals(680)) Then

                    objCtx.DeleteObject(sodet)
                    objCtx.SaveChanges()
                    Exit For
                End If


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

    }

See Also

Concepts

Application Code for Mapped Stored Procedures (Entity Framework)