如何:将更改提交到数据库How to: Submit Changes to the Database

无论您对对象做了多少项更改,都只是在更改内存中的副本。Regardless of how many changes you make to your objects, changes are made only to in-memory replicas. 您并未对数据库中的实际数据做任何更改。You have made no changes to the actual data in the database. 直到您对 SubmitChanges 显式调用 DataContext,您所做的更改才会传输到服务器。Your changes are not transmitted to the server until you explicitly call SubmitChanges on the DataContext.

当您进行此调用时,DataContext 会设法将您所做的更改转换为等效的 SQL 命令。When you make this call, the DataContext tries to translate your changes into equivalent SQL commands. 你可以使用自己的自定义逻辑来重写这些操作,但提交的顺序由DataContext称为更改处理器的服务进行协调。You can use your own custom logic to override these actions, but the order of submission is orchestrated by a service of the DataContext known as the change processor. 事件的顺序如下:The sequence of events is as follows:

  1. 当您调用 SubmitChanges 时,LINQ to SQLLINQ to SQL 会检查已知对象的集合以确定新实例是否已附加到它们。When you call SubmitChanges, LINQ to SQLLINQ to SQL examines the set of known objects to determine whether new instances have been attached to them. 如果已附加,这些新实例将添加到被跟踪对象的集合。If they have, these new instances are added to the set of tracked objects.

  2. 所有具有挂起更改的对象将按照它们之间的依赖关系排序成一个对象序列。All objects that have pending changes are ordered into a sequence of objects based on the dependencies between them. 如果一个对象的更改依赖于其他对象,则这个对象将排在其依赖项之后。Objects whose changes depend on other objects are sequenced after their dependencies.

  3. 在即将传输任何实际更改时,LINQ to SQLLINQ to SQL 会启动一个事务来包装由各条命令组成的系列。Immediately before any actual changes are transmitted, LINQ to SQLLINQ to SQL starts a transaction to encapsulate the series of individual commands.

  4. 对对象的更改会逐个转换为 SQL 命令,然后发送到服务器。The changes to the objects are translated one by one to SQL commands and sent to the server.

此时,如果数据库检测到任何错误,都会造成提交进程停止并引发异常。At this point, any errors detected by the database cause the submission process to stop, and an exception is raised. 将回滚对数据库的所有更改,就像未进行过提交一样。All changes to the database are rolled back as if no submissions ever occurred. DataContext 仍具有所有更改的完整记录。The DataContext still has a full recording of all changes. 因此你可以设法修正问题并重新调用 SubmitChanges,就像下面的代码示例中那样。You can therefore try to correct the problem and call SubmitChanges again, as in the code example that follows.

示例Example

用于执行提交的事务成功完成后,DataContext 就会通过忽略更改跟踪信息接受对对象的更改。When the transaction around the submission is completed successfully, the DataContext accepts the changes to the objects by ignoring the change-tracking information.

Northwnd db = new Northwnd(@"c:\northwnd.mdf");
// Make changes here. 
try
{
    db.SubmitChanges();
}
catch (ChangeConflictException e)
{
    Console.WriteLine(e.Message);
    // Make some adjustments.
    // ...
    // Try again.
    db.SubmitChanges();
}
Dim db As New Northwnd("c:\northwnd.mdf")

' Make changes here.
Sub MakeChanges()
    Try
        db.SubmitChanges()
    Catch e As ChangeConflictException
        Console.WriteLine(e.Message)
        ' Make some adjustments 
        '...
        ' Try again.
        db.SubmitChanges()
    End Try
End Sub

请参阅See also