Condividi tramite


How to: Add, Modify, and Delete Objects (Entity Framework)

This topic provides an example of how to use Object Services to modify objects in an object context and save the data to the database. The example in this topic is based on the Adventure Works Sales Model. To run the code in this example, you must have already added the AdventureWorks Sales Model to your project and configured your project to use the Entity Framework. To do this, complete the procedures in How to: Manually Configure an Entity Framework Project and How to: Manually Define an Entity Data Model (Entity Framework).

Example

In this example, an object query returns a single SalesOrderHeader object based on a specified SalesOrderID. The status for this order is changed from 5 (shipped) to 1 (in process), a new item is added to the order, and the first existing item is deleted. The SaveChanges method is called to write changes to the database. The resulting state of the order is then written to the console.

' Specify the order to update.
Dim orderId = 43680

Using context As New AdventureWorksEntities()
    Try
        Dim order As SalesOrderHeader = _
        context.SalesOrderHeader.Where( _
                "it.SalesOrderID = @id", New ObjectParameter( _
                 "id", orderId)).First()

        ' Change the status and ship date of an existing order.
        order.Status = 1
        order.ShipDate = DateAndTime.Today

        ' Load items for the order, if not already loaded.
        If Not order.SalesOrderDetail.IsLoaded Then
            order.SalesOrderDetail.Load()
        End If

        ' Delete the first item in the order.
        context.DeleteObject(order.SalesOrderDetail.First())

        ' Create a new item using the static Create method
        ' and add it to the order.
        order.SalesOrderDetail.Add( _
            SalesOrderDetail.CreateSalesOrderDetail( _
            1, 0, 2, 750, 1, CDec(2171.2942), 0, 0, Guid.NewGuid(), _
            DateAndTime.Today))

        ' Save changes in the object context to the database.
        Dim changes = context.SaveChanges()

        Console.WriteLine(changes.ToString() & " changes saved!")
        Console.WriteLine("Updated item for order: " _
            & order.SalesOrderID.ToString())

        Dim item As SalesOrderDetail
        For Each item In order.SalesOrderDetail
            Console.WriteLine("Item ID: " _
                & item.SalesOrderDetailID.ToString() & "  Product: " _
                & item.ProductID.ToString() & "  Quantity: " & item.OrderQty.ToString())
        Next
    Catch ex As UpdateException
        Console.WriteLine(ex.ToString())
    End Try
End Using
// Specify the order to update.
int orderId = 43680;

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    try
    {
        SalesOrderHeader order =
            context.SalesOrderHeader.Where
            ("it.SalesOrderID = @id", new ObjectParameter(
             "id", orderId)).First();

        // Change the status and ship date of an existing order.
        order.Status = 1;
        order.ShipDate = DateTime.Today;

        // Load items for the order, if not already loaded.
        if (!order.SalesOrderDetail.IsLoaded)
        {
            order.SalesOrderDetail.Load();
        }

        // Delete the first item in the order.
        context.DeleteObject(order.SalesOrderDetail.First());

        // Create a new item using the static Create method 
        // and add it to the order.
        order.SalesOrderDetail.Add(
            SalesOrderDetail.CreateSalesOrderDetail(0,
            0, 2, 750, 1, (decimal)2171.2942, 0, 0,
            Guid.NewGuid(), DateTime.Today));

        // Save changes in the object context to the database.
        int changes = context.SaveChanges();

        Console.WriteLine(changes.ToString() + " changes saved!");
        Console.WriteLine("Updated item for order: "
            + order.SalesOrderID.ToString());

        foreach (SalesOrderDetail item in order.SalesOrderDetail)
        {
            Console.WriteLine("Item ID: "
                + item.SalesOrderDetailID.ToString() + "  Product: "
                + item.ProductID.ToString() + "  Quantity: "
                + item.OrderQty.ToString());
        }
    }
    catch (UpdateException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

See Also

Concepts

Adding, Modifying, and Deleting Objects (Entity Framework)
Object Services Overview (Entity Framework)