@@ -1440,14 +1440,16 @@ private EntitySet GetEntitySetForNameAndType(string entitySetName, Type entityCL
1440
1440
/// Calls to EnsureConnection MUST be matched with a single call to ReleaseConnection.
1441
1441
/// </summary>
1442
1442
/// <exception cref="ObjectDisposedException">
1443
- /// If the
1444
- /// <see cref="ObjectContext" />
1445
- /// instance has been disposed.
1443
+ /// If the <see cref="ObjectContext" /> instance has been disposed.
1446
1444
/// </exception>
1447
1445
internal virtual void EnsureConnection ( )
1448
1446
{
1449
- if ( ConnectionState . Closed
1450
- == Connection . State )
1447
+ if ( Connection . State == ConnectionState . Broken )
1448
+ {
1449
+ Connection . Close ( ) ;
1450
+ }
1451
+
1452
+ if ( Connection . State == ConnectionState . Closed )
1451
1453
{
1452
1454
Connection . Open ( ) ;
1453
1455
_openedConnection = true ;
@@ -1458,17 +1460,6 @@ internal virtual void EnsureConnection()
1458
1460
_connectionRequestCount ++ ;
1459
1461
}
1460
1462
1461
- // Check the connection was opened correctly
1462
- if ( Connection . State == ConnectionState . Closed
1463
- || Connection . State == ConnectionState . Broken )
1464
- {
1465
- var message = Strings . EntityClient_ExecutingOnClosedConnection (
1466
- Connection . State == ConnectionState . Closed
1467
- ? Strings . EntityClient_ConnectionStateClosed
1468
- : Strings . EntityClient_ConnectionStateBroken ) ;
1469
- throw new InvalidOperationException ( message ) ;
1470
- }
1471
-
1472
1463
try
1473
1464
{
1474
1465
// Make sure the necessary metadata is registered
@@ -1507,14 +1498,16 @@ internal virtual void EnsureConnection()
1507
1498
/// Calls to EnsureConnection MUST be matched with a single call to ReleaseConnection.
1508
1499
/// </summary>
1509
1500
/// <exception cref="ObjectDisposedException">
1510
- /// If the
1511
- /// <see cref="ObjectContext" />
1512
- /// instance has been disposed.
1501
+ /// If the <see cref="ObjectContext" /> instance has been disposed.
1513
1502
/// </exception>
1514
1503
internal virtual async Task EnsureConnectionAsync ( CancellationToken cancellationToken )
1515
1504
{
1516
- if ( ConnectionState . Closed
1517
- == Connection . State )
1505
+ if ( Connection . State == ConnectionState . Broken )
1506
+ {
1507
+ Connection . Close ( ) ;
1508
+ }
1509
+
1510
+ if ( Connection . State == ConnectionState . Closed )
1518
1511
{
1519
1512
await Connection . OpenAsync ( cancellationToken ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
1520
1513
_openedConnection = true ;
@@ -1525,17 +1518,6 @@ internal virtual async Task EnsureConnectionAsync(CancellationToken cancellation
1525
1518
_connectionRequestCount ++ ;
1526
1519
}
1527
1520
1528
- // Check the connection was opened correctly
1529
- if ( Connection . State == ConnectionState . Closed
1530
- || Connection . State == ConnectionState . Broken )
1531
- {
1532
- var message = Strings . EntityClient_ExecutingOnClosedConnection (
1533
- Connection . State == ConnectionState . Closed
1534
- ? Strings . EntityClient_ConnectionStateClosed
1535
- : Strings . EntityClient_ConnectionStateBroken ) ;
1536
- throw new InvalidOperationException ( message ) ;
1537
- }
1538
-
1539
1521
try
1540
1522
{
1541
1523
// Make sure the necessary metadata is registered
@@ -2545,7 +2527,8 @@ public virtual int SaveChanges(SaveOptions options)
2545
2527
// if there are no changes to save, perform fast exit to avoid interacting with or starting of new transactions
2546
2528
if ( 0 < entriesAffected )
2547
2529
{
2548
- entriesAffected = SaveChangesToStore ( options ) ;
2530
+ var executionStrategy = DbProviderServices . GetExecutionStrategy ( Connection ) ;
2531
+ entriesAffected = executionStrategy . Execute ( ( ) => SaveChangesToStore ( options ) ) ;
2549
2532
}
2550
2533
2551
2534
ObjectStateManager . AssertAllForeignKeyIndexEntriesAreValid ( ) ;
@@ -2572,18 +2555,20 @@ public Task<Int32> SaveChangesAsync(SaveOptions options)
2572
2555
/// <param name="options"> Describes behavior options of SaveChanges </param>
2573
2556
/// <param name="cancellationToken"> The token to monitor for cancellation requests </param>
2574
2557
/// <returns> A task representing the asynchronous operation </returns>
2575
- public virtual Task < Int32 > SaveChangesAsync ( SaveOptions options , CancellationToken cancellationToken )
2558
+ public virtual async Task < Int32 > SaveChangesAsync ( SaveOptions options , CancellationToken cancellationToken )
2576
2559
{
2577
2560
PrepareToSaveChanges ( options ) ;
2578
2561
2579
2562
var entriesAffected =
2580
- Task . FromResult (
2581
- ObjectStateManager . GetObjectStateEntriesCount ( EntityState . Added | EntityState . Deleted | EntityState . Modified ) ) ;
2563
+ ObjectStateManager . GetObjectStateEntriesCount ( EntityState . Added | EntityState . Deleted | EntityState . Modified ) ;
2582
2564
2583
2565
// if there are no changes to save, perform fast exit to avoid interacting with or starting of new transactions
2584
- if ( 0 < entriesAffected . Result )
2566
+ if ( 0 < entriesAffected )
2585
2567
{
2586
- entriesAffected = SaveChangesToStoreAsync ( options , cancellationToken ) ;
2568
+ var executionStrategy = DbProviderServices . GetExecutionStrategy ( Connection ) ;
2569
+ entriesAffected = await
2570
+ executionStrategy . ExecuteAsync ( ( ) => SaveChangesToStoreAsync ( options , cancellationToken ) )
2571
+ . ConfigureAwait ( continueOnCapturedContext : false ) ;
2587
2572
}
2588
2573
2589
2574
ObjectStateManager . AssertAllForeignKeyIndexEntriesAreValid ( ) ;
@@ -2631,7 +2616,7 @@ private int SaveChangesToStore(SaveOptions options)
2631
2616
if ( _commandInterceptor == null
2632
2617
|| ! _commandInterceptor . IsEnabled )
2633
2618
{
2634
- entriesAffected = ExecuteInTransaction ( ( ) => _adapter . Update ( ObjectStateManager , true ) ) ;
2619
+ entriesAffected = ExecuteInTransaction ( ( ) => _adapter . Update ( ObjectStateManager , throwOnClosedConnection : true ) ) ;
2635
2620
}
2636
2621
else
2637
2622
{
@@ -2640,8 +2625,6 @@ private int SaveChangesToStore(SaveOptions options)
2640
2625
2641
2626
if ( ( SaveOptions . AcceptAllChangesAfterSave & options ) != 0 )
2642
2627
{
2643
- // only accept changes after the local transaction commits
2644
-
2645
2628
try
2646
2629
{
2647
2630
AcceptAllChanges ( ) ;
@@ -2689,15 +2672,13 @@ private async Task<int> SaveChangesToStoreAsync(SaveOptions options, Cancellatio
2689
2672
2690
2673
if ( ( SaveOptions . AcceptAllChangesAfterSave & options ) != 0 )
2691
2674
{
2692
- // only accept changes after the local transaction commits
2693
-
2694
2675
try
2695
2676
{
2696
2677
AcceptAllChanges ( ) ;
2697
2678
}
2698
2679
catch ( Exception e )
2699
2680
{
2700
- // If AcceptAllChanges throw - let's inform user that changes in database were committed
2681
+ // If AcceptAllChanges throws - let's inform user that changes in database were committed
2701
2682
// and that Context and Database can be in inconsistent state.
2702
2683
throw new InvalidOperationException ( Strings . ObjectContext_AcceptAllChangesFailure ( e . Message ) ) ;
2703
2684
}
@@ -3530,7 +3511,8 @@ public virtual ObjectResult<TElement> ExecuteStoreQuery<TElement>(string command
3530
3511
/// <returns>
3531
3512
/// An enumeration of objects of type <typeparamref name="TElement" /> .
3532
3513
/// </returns>
3533
- public virtual ObjectResult < TElement > ExecuteStoreQuery < TElement > ( string commandText , ExecutionOptions executionOptions , params object [ ] parameters )
3514
+ public virtual ObjectResult < TElement > ExecuteStoreQuery < TElement > (
3515
+ string commandText , ExecutionOptions executionOptions , params object [ ] parameters )
3534
3516
{
3535
3517
return ExecuteStoreQueryInternal < TElement > (
3536
3518
commandText , /*entitySetName:*/ null , executionOptions , parameters ) ;
@@ -3552,7 +3534,8 @@ public virtual ObjectResult<TElement> ExecuteStoreQuery<TElement>(
3552
3534
string commandText , string entitySetName , MergeOption mergeOption , params object [ ] parameters )
3553
3535
{
3554
3536
Check . NotEmpty ( entitySetName , "entitySetName" ) ;
3555
- return ExecuteStoreQueryInternal < TElement > ( commandText , entitySetName , new ExecutionOptions ( mergeOption , streaming : false ) , parameters ) ;
3537
+ return ExecuteStoreQueryInternal < TElement > (
3538
+ commandText , entitySetName , new ExecutionOptions ( mergeOption , streaming : false ) , parameters ) ;
3556
3539
}
3557
3540
3558
3541
/// <summary>
0 commit comments