コード例 #1
0
        public static void SeedDatabase(this GaloreDbContext _dbContext)
        {
            _dbContext.Database.EnsureCreated();

            /* ONLY COMMENT OUT WHEN DELETING ALL DATA FROM DB */
            // _dbContext.Database.ExecuteSqlCommand("TRUNCATE TABLE Reviews");
            // _dbContext.Database.ExecuteSqlCommand("TRUNCATE TABLE Loans");
            // _dbContext.Database.ExecuteSqlCommand("TRUNCATE TABLE Users");
            // _dbContext.Database.ExecuteSqlCommand("TRUNCATE TABLE Tapes");


            /* TAPES */
            _dbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Tapes ON");
            if (!_dbContext.Tapes.Any())
            {
                var tapes = new List <Tape>();
                using (StreamReader r = new StreamReader("./JsonData/Tapes_NoId.json"))
                {
                    string json = r.ReadToEnd();
                    tapes = JsonConvert.DeserializeObject <List <Tape> >(json);
                }
                _dbContext.AddRange(tapes);
                _dbContext.SaveChanges();
                _dbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Tapes OFF");
            }


            /* USERS */
            _dbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Users ON");
            if (!_dbContext.Users.Any())
            {
                var users = new List <User>();
                using (StreamReader r = new StreamReader("./JsonData/Users_NoId.json"))
                {
                    string json = r.ReadToEnd();
                    users = JsonConvert.DeserializeObject <List <User> >(json);
                }
                _dbContext.AddRange(users);
                _dbContext.SaveChanges();
                _dbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Users OFF");
            }

            _dbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Loans ON");
            if (!_dbContext.Loans.Any())
            {
                var loans = new List <Loan>();
                using (StreamReader r = new StreamReader("./JsonData/Loans.json"))
                {
                    string json = r.ReadToEnd();
                    loans = JsonConvert.DeserializeObject <List <Loan> >(json);
                }


                _dbContext.AddRange(loans);
                _dbContext.SaveChanges();
                _dbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Loans OFF");
            }
        }
コード例 #2
0
        public void Initialize()
        {
            // arrange
            var options = new DbContextOptionsBuilder <GaloreDbContext>()
                          .UseInMemoryDatabase(databaseName: "Tapes").Options;

            _context   = new GaloreDbContext(options);
            repository = new TapeRepository(_context);
        }
コード例 #3
0
        public void Initialize()
        {
            // arrange
            var options = new DbContextOptionsBuilder <GaloreDbContext>()
                          .UseInMemoryDatabase(databaseName: "Loans").Options;

            _context       = new GaloreDbContext(options);
            repository     = new LoanRepository(_context);
            userRepository = new UserRepository(_context);
            tapeRepository = new TapeRepository(_context);

            // add users for testing
            var user2 = new User
            {
                FirstName = "Asdis Erna",
                LastName  = "Gudmundsdottir",
                Email     = "*****@*****.**",
                Phone     = "5885522",
                Address   = "Kopavogsgata 3",
                Deleted   = false,
            };
            var user1 = new User
            {
                FirstName = "Unnsteinn",
                LastName  = "Gardarsson",
                Email     = "*****@*****.**",
                Phone     = "6633819",
                Address   = "Leifsgata 27",
                Deleted   = false,
            };
            var user1Id = userRepository.CreateUser(user1);
            var user2Id = userRepository.CreateUser(user2);

            // add tapes for testing
            var tape1 = new Tape
            {
                Title             = "The Shining",
                DirectorFirstName = "Stanley",
                DirectorLastName  = "Kubrick",
                Type        = "vhs",
                EIDR        = "10.5240/XXXX-XXXX-XXXX-XXXX-XXXX-C",
                ReleaseDate = new DateTime(1980, 10, 5),
                Deleted     = false,
            };
            var tape2 = new Tape
            {
                Title             = "The Lion King",
                DirectorFirstName = "Roger",
                DirectorLastName  = "Allers",
                Type        = "vhs",
                EIDR        = "10.5240/XXXX-XXXX-XXXX-XXXX-XXXX-C",
                ReleaseDate = new DateTime(1994, 12, 2),
                Deleted     = false,
            };
            var tape1Id = tapeRepository.CreateTape(tape1);
            var tape2Id = tapeRepository.CreateTape(tape2);
        }
コード例 #4
0
 public LoanRepository(GaloreDbContext dbContext)
 {
     _dbContext = dbContext;
 }
コード例 #5
0
 public UserRepository(GaloreDbContext dbContext)
 {
     _dbContext = dbContext;
 }
コード例 #6
0
 public TapeRepository(GaloreDbContext dbContext)
 {
     _dbContext = dbContext;
 }
コード例 #7
0
 public ReviewRepository(GaloreDbContext context)
 {
     _context = context;
 }