Skip to content

Commit

Permalink
Merge branch 'issue57'
Browse files Browse the repository at this point in the history
  • Loading branch information
RubberChickenParadise committed Oct 27, 2015
2 parents f80e1a6 + 9de869c commit 8dc3b6e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -299,17 +299,36 @@ public static class EfMappingFactory
{
private static Dictionary<Type, EfMapping> cache = new Dictionary<Type, EfMapping>();

private static object lckObj = new object();

public static EfMapping GetMappingsForContext(DbContext context)
{
var type = context.GetType();

EfMapping mapping;

//Try our cache lookup. If we find an item in the cache
//there is no reason to obtain a lock and limit this part
//of the code to a single thread
if (!cache.TryGetValue(type, out mapping))
{
mapping = new EfMapping(context);
cache.Add(type, mapping);
//if the cache lookup fails lock so only a single thread
//at a time can access the add code
lock (lckObj)
{
//Check the cache again. If 2 threads failed the first cache lookup
//the first thread will add the item to the cache
//the second thread will wait on the lock and then pull the item from
//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 @@ -83,7 +83,7 @@ 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)
{
var tempTableName = "temp_" + tableName + "_" + DateTime.Now.Ticks;
var tempTableName = "temp_" + tableName + "_" + Guid.NewGuid().ToString("N");
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

0 comments on commit 8dc3b6e

Please sign in to comment.