Skip to content

Commit 89ffc91

Browse files
Issue MikaelEliasson#13: Handling wrapped connections
Make it possible to provide InsertAll with a connection manually to avoid problems with wrapped connections
1 parent 158779c commit 89ffc91

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

EntityFramework.Utilities/EntityFramework.Utilities/EFBatchOperation.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Data.Common;
35
using System.Data.Entity;
46
using System.Data.Entity.Core.EntityClient;
57
using System.Data.Entity.Core.Objects;
@@ -13,7 +15,12 @@ namespace EntityFramework.Utilities
1315

1416
public interface IEFBatchOperationBase<TContext, T> where T : class
1517
{
16-
void InsertAll(IEnumerable<T> items);
18+
/// <summary>
19+
/// Bulk insert all items if the Provider supports it. Otherwise it will use the default insert unless Configuration.DisableDefaultFallback is set to true in which case it would throw an exception.
20+
/// </summary>
21+
/// <param name="items">The items to insert</param>
22+
/// <param name="connection">The DbConnection to use for the insert. Only needed when for example a profiler wraps the connection. Then you need to provide a connection of the type the provider use.</param>
23+
void InsertAll(IEnumerable<T> items, DbConnection connection = null);
1724
IEFBatchOperationFiltered<TContext, T> Where(Expression<Func<T, bool>> predicate);
1825
}
1926

@@ -54,7 +61,12 @@ public static IEFBatchOperationBase<TContext, T> For<TContext, T>(TContext conte
5461
return new EFBatchOperation<TContext, T>(context, set);
5562
}
5663

57-
public void InsertAll(IEnumerable<T> items)
64+
/// <summary>
65+
/// Bulk insert all items if the Provider supports it. Otherwise it will use the default insert unless Configuration.DisableDefaultFallback is set to true in which case it would throw an exception.
66+
/// </summary>
67+
/// <param name="items">The items to insert</param>
68+
/// <param name="connection">The DbConnection to use for the insert. Only needed when for example a profiler wraps the connection. Then you need to provide a connection of the type the provider use.</param>
69+
public void InsertAll(IEnumerable<T> items, DbConnection connection = null)
5870
{
5971
var con = context.Connection as EntityConnection;
6072
if (con == null)
@@ -63,7 +75,9 @@ public void InsertAll(IEnumerable<T> items)
6375
Fallbacks.DefaultInsertAll(context, items);
6476
}
6577

66-
var provider = Configuration.Providers.FirstOrDefault(p => p.CanHandle(con.StoreConnection));
78+
var connectionToUse = connection ?? con.StoreConnection;
79+
80+
var provider = Configuration.Providers.FirstOrDefault(p => p.CanHandle(connectionToUse));
6781
if (provider != null && provider.CanInsert)
6882
{
6983

@@ -73,11 +87,11 @@ public void InsertAll(IEnumerable<T> items)
7387

7488
var properties = tableMapping.PropertyMappings.Select(p => new ColumnMapping { NameInDatabase = p.ColumnName, NameOnObject = p.PropertyName }).ToList();
7589

76-
provider.InsertItems(items, tableMapping.Schema, tableMapping.TableName, properties, con.StoreConnection);
90+
provider.InsertItems(items, tableMapping.Schema, tableMapping.TableName, properties, connectionToUse);
7791
}
7892
else
7993
{
80-
Configuration.Log("Found provider: " + (provider == null ? "[]" : provider.GetType().Name) + " for " + con.StoreConnection.GetType().Name);
94+
Configuration.Log("Found provider: " + (provider == null ? "[]" : provider.GetType().Name) + " for " + connectionToUse.GetType().Name);
8195
Fallbacks.DefaultInsertAll(context, items);
8296
}
8397
}

EntityFramework.Utilities/Tests/InsertTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,31 @@ public void InsertAll_InsertsItems()
3636
}
3737
}
3838

39+
[TestMethod]
40+
public void InsertAll_WithExplicitConnection_InsertsItems()
41+
{
42+
using (var db = Context.Sql())
43+
{
44+
if (db.Database.Exists())
45+
{
46+
db.Database.Delete();
47+
}
48+
db.Database.Create();
49+
50+
var list = new List<BlogPost>(){
51+
BlogPost.Create("T1"),
52+
BlogPost.Create("T2"),
53+
BlogPost.Create("T3")
54+
};
55+
EFBatchOperation.For(db, db.BlogPosts).InsertAll(list, db.Database.Connection);
56+
}
57+
58+
using (var db = Context.Sql())
59+
{
60+
Assert.AreEqual(3, db.BlogPosts.Count());
61+
}
62+
}
63+
3964
[TestMethod]
4065
public void InsertAll_WrongColumnOrder_InsertsItems()
4166
{

EntityFramework.Utilities/Tests/Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<Reference Include="System.Core">
5454
<RequiredTargetFramework>3.5</RequiredTargetFramework>
5555
</Reference>
56+
<Reference Include="System.Data" />
5657
<Reference Include="System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL">
5758
<Private>True</Private>
5859
<HintPath>..\packages\Microsoft.SqlServer.Compact.4.0.8876.1\lib\net40\System.Data.SqlServerCe.dll</HintPath>

0 commit comments

Comments
 (0)