public void TestToCreateDatebase()
 {
     using (var context = new MachineDataContext(GetOptions(), null))
     {
         var machines = context.Machines.ToList();
         Assert.IsNotNull(machines);
         var count = context.Machines.Count();
         Assert.AreEqual(1, count);
     } // Dispose
 }
        public void TestInitialize()
        {
            var init = new DatabaseInitializer();

            using (var context = new MachineDataContext(GetOptions(), null))
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();
                init.Seed(context);
            } // Dispose
        }
Exemple #3
0
        public DatabaseInitializer(IServiceProvider provider)
        {
            this.repIdentityRole      = provider.GetService <IAuthenticationRepository <IdentityRole, string> >();
            this.repIdentityUser      = provider.GetService <IAuthenticationRepository <IdentityUser, string> >();
            this.repIdentityUserRole  = provider.GetService <IAuthenticationRepository <IdentityUserRole <string>, string> >();
            this.repIdentityUserClaim = provider.GetService <IAuthenticationRepository <IdentityUserClaim <string>, string> >();
            this.repMachine           = provider.GetService <IGenericRepository <Machine, int> >();
            this.repDevice            = provider.GetService <IGenericRepository <Device, int> >();
            this.repDatavalue         = provider.GetService <IGenericRepository <DataValue, int> >();
            this.repOperators         = provider.GetService <IGenericRepository <MachineOperator, int> >();

            this.authContext    = provider.GetService <AuthenticationDataContext>();
            this.machineContext = provider.GetService <MachineDataContext>();
        }
 public GenericDbRepository(MachineDataContext context)
 {
     Context = context;
 }
        internal void Seed(MachineDataContext context)
        {
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();
            // Demo user and admin for frontend administration
            var guestRole = new ApplicationRole {
                Name = "Guest", Id = Guid.NewGuid().ToString("N")
            };
            var userRole = new ApplicationRole {
                Name = "User", Id = Guid.NewGuid().ToString("N")
            };
            var adminRole = new ApplicationRole {
                Name = "Admin", Id = Guid.NewGuid().ToString("N")
            };

            context.Roles.AddRange(new [] { guestRole, userRole, adminRole });
            var hasher = new Microsoft.AspNet.Identity.PasswordHasher();
            var guest  = new ApplicationUser
            {
                UserName     = "******",
                Id           = Guid.NewGuid().ToString("N"),
                Email        = "*****@*****.**",
                PasswordHash = hasher.HashPassword("p@ssw0rd")
            };
            var user = new ApplicationUser
            {
                UserName     = "******",
                Id           = Guid.NewGuid().ToString("N"),
                Email        = "*****@*****.**",
                PasswordHash = hasher.HashPassword("p@ssw0rd")
            };
            var admin = new ApplicationUser
            {
                UserName     = "******",
                Id           = Guid.NewGuid().ToString("N"),
                Email        = "*****@*****.**",
                PasswordHash = hasher.HashPassword("p@ssw0rd")
            };

            context.Users.Add(user);
            context.Users.Add(admin);
            // Assign users to roles
            var guestUserRole = new IdentityUserRole <string> {
                UserId = guest.Id, RoleId = guestRole.Id
            };
            var userUserRole = new IdentityUserRole <string> {
                UserId = user.Id, RoleId = userRole.Id
            };
            var adminUserRole = new IdentityUserRole <string> {
                UserId = admin.Id, RoleId = adminRole.Id
            };

            context.UserRoles.AddRange(new[] { guestUserRole, userUserRole, adminUserRole });
            context.SaveChanges();
            // Demo data
            var machine = new Machine {
                Name     = "M1",
                Location = "A-100"
            };
            var device1 = new Device {
                Name = "BG A"
            };
            var device2 = new Device {
                Name = "BG B"
            };
            var v1 = new DataValue {
                Value = 123.5, Unit = "V"
            };
            var v2 = new DataValue {
                Value = 43, Unit = "V"
            };
            var v3 = new DataValue {
                Value = 230, Unit = "V"
            };
            var v4 = new DataValue {
                Value = 380, Unit = "V"
            };
            var v5 = new DataValue {
                Value = 0.005, Unit = "A"
            };

            machine.Devices.Add(device1);
            machine.Devices.Add(device2);
            device1.DataValues.Add(v1);
            device1.DataValues.Add(v2);
            device2.DataValues.Add(v3);
            device2.DataValues.Add(v4);
            device2.DataValues.Add(v5);

            context.Machines.Add(machine);
            context.SaveChanges();
        }