diff --git a/EntityFramework.Utilities/EntityFramework.Utilities/MappingHelper.cs b/EntityFramework.Utilities/EntityFramework.Utilities/MappingHelper.cs index 1100b1a..30883b0 100644 --- a/EntityFramework.Utilities/EntityFramework.Utilities/MappingHelper.cs +++ b/EntityFramework.Utilities/EntityFramework.Utilities/MappingHelper.cs @@ -299,17 +299,36 @@ public static class EfMappingFactory { private static Dictionary cache = new Dictionary(); + 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; } - } } diff --git a/EntityFramework.Utilities/EntityFramework.Utilities/SqlQueryProvider.cs b/EntityFramework.Utilities/EntityFramework.Utilities/SqlQueryProvider.cs index 975ae1a..f08bb6d 100644 --- a/EntityFramework.Utilities/EntityFramework.Utilities/SqlQueryProvider.cs +++ b/EntityFramework.Utilities/EntityFramework.Utilities/SqlQueryProvider.cs @@ -83,7 +83,7 @@ public void InsertItems(IEnumerable items, string schema, string tableName public void UpdateItems(IEnumerable items, string schema, string tableName, IList properties, DbConnection storeConnection, int? batchSize, UpdateSpecification 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);