/// <summary> /// Methods of adding a record in the database (Storage1). /// Called from the methods TransactionScopeWithStorage. /// </summary> /// <param name="context">The context of the interaction with the database.</param> /// <param name="entity">The object to add the type of User.</param> /// <param name="count">Counter.</param> /// <param name="commitCount">Meaning - "every time commitCount doing SaveChanges()".</param> /// <param name="recreateContext">It must be true.</param> /// <returns>The context of the interaction with the database.</returns> private static Storage1Context AddToStorage1(Storage1Context context, User entity, int count, int commitCount, bool recreateContext) { context.Set<User>().Add(entity); if (count%commitCount != 0) return context; context.SaveChanges(); if (!recreateContext) return context; context.Dispose(); context = new Storage1Context(); context.Configuration.AutoDetectChangesEnabled = false; return context; }
/// <summary> /// The method for forming transactions with Storage1. /// Called from an event handler btnFill_Click. /// </summary> /// <param name="users">The list of users you want to add.</param> public void TransactionScopeWithStorage1(List<User> users) { using (var scope = new TransactionScope()) { Storage1Context context = null; try { context = new Storage1Context(); context.Configuration.AutoDetectChangesEnabled = false; var count = 0; foreach (var entityToInsert in users) { ++count; context = AddToStorage1(context, entityToInsert, count, 100, true); } context.SaveChanges(); } finally { context?.Dispose(); } scope.Complete(); } }
/// <summary> /// Event: Make all friends. /// </summary> /// <param name="sender">Sender</param> /// <param name="e">Event Args</param> override protected void btnFriends_Click(object sender, RoutedEventArgs e) { var arrayNumberFriends = new int[500000]; for (var i = 0; i < 500000; i++) arrayNumberFriends[i] = 0; var rnd = new Random(); var storage1Context = new Storage1Context(); var storage2Context = new Storage2Context(); var storage3Context = new Storage3Context(); var storage4Context = new Storage4Context(); var storage5Context = new Storage5Context(); storage1Context.Configuration.AutoDetectChangesEnabled = false; storage2Context.Configuration.AutoDetectChangesEnabled = false; storage3Context.Configuration.AutoDetectChangesEnabled = false; storage4Context.Configuration.AutoDetectChangesEnabled = false; storage5Context.Configuration.AutoDetectChangesEnabled = false; for (var i = 0; i < 500000; i++) { if (arrayNumberFriends[i] == 5) continue; var shortage = 5 - arrayNumberFriends[i]; for (var j = 0; j < shortage; j++) { var friendId = rnd.Next(1, 500001); if (i == friendId) friendId = rnd.Next(1, 500001); if (i <= 100000) { storage1Context.Friends.Add(new Friends { User_Id = i + 1, Friend_Id = friendId }); storage1Context.SaveChanges(); arrayNumberFriends[i]++; } else if (i > 100000 && i < 200000) { storage2Context.Friends.Add(new Friends { User_Id = i, Friend_Id = friendId }); storage2Context.SaveChanges(); arrayNumberFriends[i]++; } else if (i > 200000 && i < 300000) { storage3Context.Friends.Add(new Friends { User_Id = i, Friend_Id = friendId }); storage3Context.SaveChanges(); arrayNumberFriends[i]++; } else if (i > 300000 && i < 400000) { storage4Context.Friends.Add(new Friends { User_Id = i, Friend_Id = friendId }); storage4Context.SaveChanges(); arrayNumberFriends[i]++; } else if (i > 400000 && i < 500000) { storage5Context.Friends.Add(new Friends { User_Id = i, Friend_Id = friendId }); storage5Context.SaveChanges(); arrayNumberFriends[i]++; } if (friendId > 1 && friendId < 100000) { storage1Context.Friends.Add(new Friends { User_Id = friendId, Friend_Id = (i <= 100000) ? i + 1 : i }); storage1Context.SaveChanges(); arrayNumberFriends[friendId]++; } else if (friendId > 100000 && friendId < 200000) { storage2Context.Friends.Add(new Friends { User_Id = friendId, Friend_Id = (i <= 100000) ? i + 1 : i }); storage2Context.SaveChanges(); arrayNumberFriends[friendId]++; } else if (friendId > 200000 && friendId < 300000) { storage3Context.Friends.Add(new Friends { User_Id = friendId, Friend_Id = (i <= 100000) ? i + 1 : i }); storage3Context.SaveChanges(); arrayNumberFriends[friendId]++; } else if (friendId > 300000 && friendId < 400000) { storage4Context.Friends.Add(new Friends { User_Id = friendId, Friend_Id = (i <= 100000) ? i + 1 : i }); storage4Context.SaveChanges(); arrayNumberFriends[friendId]++; } else if (friendId > 400000 && friendId < 500000) { storage5Context.Friends.Add(new Friends { User_Id = friendId, Friend_Id = (i <= 100000) ? i + 1 : i }); storage5Context.SaveChanges(); arrayNumberFriends[friendId]++; } } } }