protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); }
public void Dispose() { if (context != null) { context.Dispose(); GC.SuppressFinalize(this); } }
protected virtual void Dispose(bool disposing) { if (!_disposed) { if (disposing) { _context.Dispose(); } _disposed = true; } }
public virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { context.Dispose(); } } this.disposed = true; }
private static void ImportPostsFromFile(List <PostDto> posts) { Console.Write("Importing posts"); var ctx = new SocialNetworkContext(); var counter = 0; var users = ctx.Users.ToList(); foreach (var postDto in posts) { var taggedUsers = postDto.Users.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); var postToAdd = new Post() { Content = postDto.Content, PostDate = postDto.PostedOn, TaggedUsers = new List <User>() }; var usersToAddTagTo = new List <User>(); var dict = new Dictionary <User, List <Post> >(); foreach (var taggedUser in taggedUsers) { var user = (User)users.FirstOrDefault(u => u.Username == taggedUser); usersToAddTagTo.Add(user); user.PostsTaggedIn.Add(postToAdd); if (!dict.ContainsKey(user)) { dict.Add(user, new List <Post>() { postToAdd }); } else { dict[user].Add(postToAdd); } } postToAdd.TaggedUsers.ToList().AddRange(usersToAddTagTo); counter++; if (counter % 10 == 0) { Console.Write("."); } if (counter % 1000 == 0) { foreach (var entry in dict) { ctx.Users.FirstOrDefault(u => u.Username == entry.Key.Username) .PostsTaggedIn.ToList() .AddRange(entry.Value); } ctx.SaveChanges(); ctx.Dispose(); ctx = new SocialNetworkContext(); users = ctx.Users.ToList(); } } Console.WriteLine(); }
private static void ImportFriendshipsFromFile(List <FriendshipDto> friendships) { var ctx = new SocialNetworkContext(); var users = ctx.Users.ToList(); var counter = 0; Console.Write("Importing users, freindships"); foreach (var friendship in friendships) { var firstUser = friendship.FirstUser; var secondUser = friendship.SecondUser; var check = CheckUserToAdd(users, firstUser, ctx); var check1 = CheckUserToAdd(users, secondUser, ctx); if (check || check1) { ctx.SaveChanges(); } else { continue; } counter++; var firstUserFriendship = ctx.Users.Select(u => new { u.Id, u.Username }).FirstOrDefault(u => u.Username == firstUser.Username); var secondUserFriendship = ctx.Users.Select(u => new { u.Id, u.Username }).FirstOrDefault(u => u.Username == secondUser.Username); var friendshipToAdd = new Friendship() { FirstUserId = firstUserFriendship.Id, SecondUserId = secondUserFriendship.Id, ApprovalDate = friendship.FriendsDate, IsApproved = bool.Parse(friendship.IsApproved), Messages = new List <Message>() }; ctx.Friendships.Add(friendshipToAdd); var messages = friendship.Messages; foreach (var messageDto in messages) { var author = ctx.Users.Select(u => new { u.Id, u.Username }).FirstOrDefault(a => a.Username == messageDto.Author); var messageToAdd = new Message() { AuthorId = author.Id, Content = messageDto.Content, SendDate = messageDto.SentOn, SeenDate = messageDto.SeenOn }; ctx.Messages.Add(messageToAdd); } ctx.SaveChanges(); if (counter % 10 == 0) { Console.Write("."); } if (counter % 100 == 0) { ctx.SaveChanges(); ctx.Dispose(); ctx = new SocialNetworkContext(); } } Console.WriteLine(); }
public void Dispose() { _context?.Dispose(); }
public void ParseFriendShipData() { Console.Write("Parsing friendships and adding to database:"); // Get all elements var friendships = xMLDocFriendships.Root.Elements("Friendship").ToList(); cnt = 0; foreach (XElement friendship in friendships) { // prepare object to write var friendshipToAdd = new Friendship(); // get friendship date if (friendship.Attributes().First().Value == "true") { friendshipToAdd.ApprovedDate = DateTime.Parse(friendship.Elements("FriendsSince").First().Value); } else { friendshipToAdd.ApprovedDate = null; } // create first user to write var firstUser = new User(); firstUser.Username = friendship.Element("FirstUser").Element("Username").Value; try { firstUser.FirstName = friendship.Element("FirstUser").Element("FirstName").Value; } catch (NullReferenceException) { // no nothing if element do not exist } try { firstUser.Lastname = friendship.Element("FirstUser").Element("Lastname").Value; } catch (NullReferenceException) { // no nothing if element do not exist } // get registration date for first user firstUser.RegistrationDate = DateTime.Parse(friendship.Element("FirstUser").Element("RegisteredOn").Value); //foreach (XElement image in friendship.Element("FirstUser").Element("Images").Elements("Image")) //{ // user.Images.Add(new Image() // { // Url = image.Element("ImageUrl").Value, // Extension = image.Element("FileExtension").Value // }); //} var firstUserFromList = users.FirstOrDefault(x => x.Username == firstUser.Username); if (firstUserFromList == null) { friendshipToAdd.FirstUser = firstUser; users.Add(firstUser); } else { friendshipToAdd.FirstUser = firstUserFromList; } // get second user var secondUser = new User(); secondUser.Username = friendship.Element("SecondUser").Element("Username").Value; try { secondUser.FirstName = friendship.Element("SecondUser").Element("FirstName").Value; } catch (NullReferenceException) { // no nothing if element do not exist } try { secondUser.Lastname = friendship.Element("SecondUser").Element("Lastname").Value; } catch (NullReferenceException) { // no nothing if element do not exist } secondUser.RegistrationDate = DateTime.Parse(friendship.Element("SecondUser").Element("RegisteredOn").Value); //foreach (XElement image in friendship.Element("SecondUser").Element("Images").Elements("Image")) //{ // user2.Images.Add(new Image() // { // Url = image.Element("ImageUrl").Value, // Extension = image.Element("FileExtension").Value // }); //} var secondUserFromList = users.FirstOrDefault(x => x.Username == secondUser.Username); if (secondUserFromList == null) { friendshipToAdd.SecondUser = secondUser; users.Add(secondUser); } else { friendshipToAdd.SecondUser = secondUserFromList; } db.Friendships.Add(friendshipToAdd); db.SaveChanges(); Console.Write("."); } db.Dispose(); db = new SocialNetworkContext(); }
public void TestCleanup() { context.Dispose(); }