Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrency Issues / TPH Problem & Transaction support #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,4 @@ pip-log.txt
# Mac crap
.DS_Store
packages
/.vs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface IEFBatchOperationBase<TContext, T> where T : class
/// <param name="items">The items to insert</param>
/// <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>
/// <param name="batchSize">The size of each batch. Default depends on the provider. SqlProvider uses 15000 as default</param>
void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connection = null, int? batchSize = null) where TEntity : class, T;
void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connection = null, int? batchSize = null, SqlTransaction transaction = null) where TEntity : class, T;
IEFBatchOperationFiltered<TContext, T> Where(Expression<Func<T, bool>> predicate);


Expand All @@ -33,7 +33,7 @@ public interface IEFBatchOperationBase<TContext, T> where T : class
/// <param name="updateSpecification">Define which columns to update</param>
/// <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>
/// <param name="batchSize">The size of each batch. Default depends on the provider. SqlProvider uses 15000 as default</param>
void UpdateAll<TEntity>(IEnumerable<TEntity> items, Action<UpdateSpecification<TEntity>> updateSpecification, DbConnection connection = null, int? batchSize = null) where TEntity : class, T;
void UpdateAll<TEntity>(IEnumerable<TEntity> items, Action<UpdateSpecification<TEntity>> updateSpecification, DbConnection connection = null, int? batchSize = null, SqlTransaction transaction = null) where TEntity : class, T;
}

public class UpdateSpecification<T>
Expand Down Expand Up @@ -95,7 +95,7 @@ public static IEFBatchOperationBase<TContext, T> For<TContext, T>(TContext conte
/// <param name="items">The items to insert</param>
/// <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>
/// <param name="batchSize">The size of each batch. Default depends on the provider. SqlProvider uses 15000 as default</param>
public void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connection = null, int? batchSize = null) where TEntity : class, T
public void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connection = null, int? batchSize = null, SqlTransaction transaction=null) where TEntity : class, T
{
var con = context.Connection as EntityConnection;
if (con == null && connection == null)
Expand Down Expand Up @@ -126,7 +126,7 @@ public void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connecti
});
}

provider.InsertItems(items, tableMapping.Schema, tableMapping.TableName, properties, connectionToUse, batchSize);
provider.InsertItems(items, tableMapping.Schema, tableMapping.TableName, properties, connectionToUse, batchSize, transaction);
}
else
{
Expand All @@ -136,7 +136,7 @@ public void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connecti
}


public void UpdateAll<TEntity>(IEnumerable<TEntity> items, Action<UpdateSpecification<TEntity>> updateSpecification, DbConnection connection = null, int? batchSize = null) where TEntity : class, T
public void UpdateAll<TEntity>(IEnumerable<TEntity> items, Action<UpdateSpecification<TEntity>> updateSpecification, DbConnection connection = null, int? batchSize = null, SqlTransaction transaction = null) where TEntity : class, T
{
var con = context.Connection as EntityConnection;
if (con == null && connection == null)
Expand Down Expand Up @@ -166,7 +166,7 @@ public void UpdateAll<TEntity>(IEnumerable<TEntity> items, Action<UpdateSpecific

var spec = new UpdateSpecification<TEntity>();
updateSpecification(spec);
provider.UpdateItems(items, tableMapping.Schema, tableMapping.TableName, properties, connectionToUse, batchSize, spec);
provider.UpdateItems(items, tableMapping.Schema, tableMapping.TableName, properties, connectionToUse, batchSize, spec, transaction);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

Expand All @@ -15,8 +16,8 @@ public interface IQueryProvider

string GetDeleteQuery(QueryInformation queryInformation);
string GetUpdateQuery(QueryInformation predicateQueryInfo, QueryInformation modificationQueryInfo);
void InsertItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize);
void UpdateItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize, UpdateSpecification<T> updateSpecification);
void InsertItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize, SqlTransaction transaction);
void UpdateItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize, UpdateSpecification<T> updateSpecification, SqlTransaction transaction);

bool CanHandle(DbConnection storeConnection);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public EfMapping(DbContext db)
}

//Inheriting propertymappings contains duplicates for id's.
tableMapping.PropertyMappings = tableMapping.PropertyMappings.GroupBy(p => p.PropertyName)
tableMapping.PropertyMappings = tableMapping.PropertyMappings.GroupBy(p => p.ColumnName)
.Select(g => g.OrderByDescending(outer => g.Count(inner => inner.ForEntityType.IsSubclassOf(outer.ForEntityType))).First())
.ToList();
foreach (var item in tableMapping.PropertyMappings)
Expand Down Expand Up @@ -305,11 +305,17 @@ public static EfMapping GetMappingsForContext(DbContext context)
EfMapping mapping;
if (!cache.TryGetValue(type, out mapping))
{
mapping = new EfMapping(context);
cache.Add(type, mapping);
//lock only if we don't have the item in the cache
lock (cache)
{
if (!cache.TryGetValue(type, out mapping))
{
mapping = new EfMapping(context);
cache.Add(type, mapping);
}
}
}
return mapping;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public string GetUpdateQuery(QueryInformation predicateQueryInfo, QueryInformati
return string.Format("UPDATE [{0}].[{1}] SET {2} {3}", predicateQueryInfo.Schema, predicateQueryInfo.Table, updateSql, predicateQueryInfo.WhereSql);
}

public void InsertItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize)
public void InsertItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize, SqlTransaction transaction)
{
using (var reader = new EFDataReader<T>(items, properties))
{
Expand All @@ -56,7 +56,7 @@ public void InsertItems<T>(IEnumerable<T> items, string schema, string tableName
{
con.Open();
}
using (SqlBulkCopy copy = new SqlBulkCopy(con))
using (SqlBulkCopy copy = new SqlBulkCopy(con, SqlBulkCopyOptions.Default, transaction))
{
copy.BatchSize = Math.Min(reader.RecordsAffected, batchSize ?? 15000); //default batch size
if (!string.IsNullOrWhiteSpace(schema))
Expand All @@ -81,9 +81,9 @@ public void InsertItems<T>(IEnumerable<T> items, string schema, string tableName
}


public void UpdateItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize, UpdateSpecification<T> updateSpecification)
public void UpdateItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize, UpdateSpecification<T> updateSpecification, SqlTransaction transaction)
{
var tempTableName = "temp_" + tableName + "_" + DateTime.Now.Ticks;
var tempTableName = "temp_" + tableName + "_" + Guid.NewGuid();
var columnsToUpdate = updateSpecification.Properties.Select(p => p.GetPropertyName()).ToDictionary(x => x);
var filtered = properties.Where(p => columnsToUpdate.ContainsKey(p.NameOnObject) || p.IsPrimaryKey).ToList();
var columns = filtered.Select(c => "[" + c.NameInDatabase + "] " + c.DataType);
Expand Down Expand Up @@ -115,7 +115,7 @@ INNER JOIN
using (var dCommand = new SqlCommand(string.Format("DROP table {0}.[{1}]", schema, tempTableName), con))
{
createCommand.ExecuteNonQuery();
InsertItems(items, schema, tempTableName, filtered, storeConnection, batchSize);
InsertItems(items, schema, tempTableName, filtered, storeConnection, batchSize, transaction);
mCommand.ExecuteNonQuery();
dCommand.ExecuteNonQuery();
}
Expand Down