コード例 #1
0
        private static void SeedUsers(SotialNetworkDbContext db)
        {
            Console.WriteLine($"Adding db...");

            var allUsers = new List <Users>();

            for (int i = 0; i < 10; i++)
            {
                var user = new Users
                {
                    Name             = $"Pesho{i}",
                    Password         = "******",
                    Email            = $"ddddd.efe@abv{i}.bg",
                    RegistratedOn    = DateTime.Now.AddDays(-(100 + i * 10)),
                    LastTimeLoggedIn = DateTime.Now.AddDays(-i),
                    Age = 22
                };

                allUsers.Add(user);
                db.Users.Add(user);
            }

            db.SaveChanges();

            var userId = allUsers
                         .Select(u => u.Id)
                         .ToList();

            for (int i = 0; i < userId.Count; i++)
            {
                var currentUserId = userId[i];

                Random random = new Random();

                var totalFriends = random.Next(1, 20);

                for (int j = 0; j < totalFriends; j++)
                {
                    var friendId = userId[random.Next(0, userId.Count)];

                    bool validFriendship = true;
                    if (friendId == currentUserId)
                    {
                        validFriendship = false;
                    }

                    var friendShipExists = db
                                           .FriendShip
                                           .Any(f => (f.FromUserId == currentUserId && f.ToUserId == friendId) ||
                                                (f.FromUserId == friendId && f.ToUserId == currentUserId));
                    if (friendShipExists)
                    {
                        validFriendship = false;
                    }
                    if (!validFriendship)
                    {
                        continue;
                    }

                    db.FriendShip.Add(new FriendShip
                    {
                        FromUserId = currentUserId,
                        ToUserId   = friendId
                    });

                    db.SaveChanges();
                }
            }

            Console.WriteLine("Db added!");
        }
コード例 #2
0
        private static void SeedAlbumsAndPictures(SotialNetworkDbContext db)
        {
            int biggestAlbId = db.Album
                               .OrderByDescending(a => a.Id)
                               .Select(a => a.Id)
                               .FirstOrDefault() + 1;

            List <int> userIds = db.Users
                                 .Select(u => u.Id)
                                 .ToList();

            Random random = new Random();

            List <Album> albums = new List <Album>();

            for (int i = biggestAlbId; i < biggestAlbId + 100; i++)
            {
                var album = new Album
                {
                    Name            = $"Album {i}",
                    BackgroundColor = $"Color {i}",
                    IsPublic        = random.Next(0, 2) == 0 ? true : false,
                    UserId          = userIds[random.Next(0, userIds.Count)]
                };
                albums.Add(album);
                db.Album.Add(album);
            }

            db.SaveChanges();

            int biggestPicture = db.Picture
                                 .Select(p => p.Id)
                                 .FirstOrDefault() + 1;

            List <Picture> pictures = new List <Picture>();

            for (int i = biggestPicture; i < biggestPicture + 500; i++)
            {
                Picture picture = new Picture
                {
                    Title   = $"Picture {i}",
                    Caption = $"Caption {i}",
                    Path    = $"Path {i}"
                };
                pictures.Add(picture);
                db.Picture.Add(picture);
            }
            db.SaveChanges();

            //List<int> albumIds = albums.Select(a => a.Id).ToList();
            //for (int i = 0; i < pictures.Count; i++)
            //{
            //    Picture picture = pictures[i];
            //    int numberOfAlbums = random.Next(1,20);
            //    for (int j = 0; j < numberOfAlbums; j++)
            //    {
            //        int albumId = albumIds[random.Next(0, albumIds.Count)];

            //        var pictureExistInAlbum = db.Picture
            //            .Any(p => p.Id == picture.Id && p.Albums.Any(a => a.AlbumId == albumId));

            //        if (pictureExistInAlbum)
            //        {
            //            continue;
            //        }

            //        picture.Albums.Add(new AlbumPicture
            //        {
            //            AlbumId=albumId
            //        });
            //    }
            //    db.SaveChanges();
            //}
        }