如何:添加、修改和删除对象(实体框架)

本主题提供的示例介绍如何使用对象服务在对象上下文中修改对象并将数据保存到数据库中。本主题中的示例基于 Adventure Works 销售模型。若要运行本示例中的代码,必须已将 AdventureWorks 销售模型添加到您的项目中,并将项目配置为使用 Entity Framework。为此,请完成如何:手动配置实体框架项目如何:手动定义实体数据模型(实体框架) 中的过程。

示例

在本示例中,对象查询根据指定的 SalesOrderID 返回单个 SalesOrderHeader 对象。此订单的状态从 5(已发货)更改为 1(在制品),向订单中添加一个新项,并删除第一个现有项。将调用 SaveChanges 方法以将更改写到数据库中。然后,将订单的结果状态写到控制台。

' Specify the order to update.
Dim orderId As Integer = 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 As Integer = 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());
    }
}

另请参见

概念

添加、修改和删除对象(实体框架)
对象服务概述(实体框架)