作法:指定並行例外狀況的擲回時機How to: Specify When Concurrency Exceptions are Thrown

LINQ to SQLLINQ to SQL 中,因為開放式並行存取 (Optimistic Concurrency) 衝突而未更新物件時,會擲回 ChangeConflictException 例外狀況 (Exception)。In LINQ to SQLLINQ to SQL, a ChangeConflictException exception is thrown when objects do not update because of optimistic concurrency conflicts. 如需詳細資訊, 請參閱開放式平行存取:總覽For more information, see Optimistic Concurrency: Overview.

將變更提交至資料庫之前,您可以指定何時應擲出並行例外狀況:Before you submit your changes to the database, you can specify when concurrency exceptions should be thrown:

  • 在第一次失敗時擲出例外狀況 (FailOnFirstConflict)。Throw the exception at the first failure (FailOnFirstConflict).

  • 完成所有更新嘗試、累積所有失敗,並且在例外狀況中報告所累積的失敗 (ContinueOnConflict)。Finish all update tries, accumulate all failures, and report the accumulated failures in the exception (ContinueOnConflict).

擲出後,ChangeConflictException 例外狀況會提供 ChangeConflictCollection 集合的存取權。When thrown, the ChangeConflictException exception provides access to a ChangeConflictCollection collection. 此集合會提供每個衝突的詳細資訊 (對應至失敗的單一更新嘗試),其中包含 MemberConflicts 集合的存取權。This collection provides details for each conflict (mapped to a single failed update try), including access to the MemberConflicts collection. 每個成員衝突都會對應至未通過並行存取檢查之更新中的單一成員。Each member conflict maps to a single member in the update that failed the concurrency check.

範例Example

下列程式碼會顯示有兩個值的範例。The following code shows examples of both values.

Northwnd db = new Northwnd("...");

// Create, update, delete code.

db.SubmitChanges(ConflictMode.FailOnFirstConflict);
// or
db.SubmitChanges(ConflictMode.ContinueOnConflict);
Dim db As New Northwnd("...")

' Create, update, delete code.

db.SubmitChanges(ConflictMode.FailOnFirstConflict)
' or
db.SubmitChanges(ConflictMode.ContinueOnConflict)

另請參閱See also