private void CreateEditions() { var defaultEdition = _context.Editions.IgnoreQueryFilters().FirstOrDefault(e => e.Name == EditionManager.DefaultEditionName); if (defaultEdition == null) { defaultEdition = new Edition { Name = EditionManager.DefaultEditionName, DisplayName = EditionManager.DefaultEditionName }; _context.Editions.Add(defaultEdition); _context.SaveChanges(); /* Add desired features to the standard edition, if wanted... */ } }
public static TemplateDbContext Create() { var options = new DbContextOptionsBuilder <TemplateDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var context = new TemplateDbContext(options); context.Database.EnsureCreated(); context.TemplateEntities.AddRange(new[] { new TemplateEntity { Id = 1, Name = "Test1" }, new TemplateEntity { Id = 2, Name = "Test2" }, new TemplateEntity { Id = 3, Name = "Test3" }, }); context.SaveChanges(); return(context); }
/// <summary> /// Initializes a new instance of the <see cref="GetRolesByUserId"/> class. /// Seeds the database. /// </summary> public GetRolesByUserId() { // Create a service provider to be shared by all test methods var serviceProvider = new ServiceCollection().AddEntityFrameworkInMemoryDatabase().BuildServiceProvider(); // Create options telling the context to use an // InMemory database and the service provider. var builder = new DbContextOptionsBuilder <TemplateDbContext>(); builder.UseInMemoryDatabase().UseInternalServiceProvider(serviceProvider); this.contextOptions = builder.Options; // seed in constructor. using (var context = new TemplateDbContext(this.contextOptions)) { string password = "******"; var roleStore = new RoleStore <IdentityRole>(context); var user = new ApplicationUser { Name = "User for test purposes", UserName = "******", NormalizedUserName = "******", Email = "*****@*****.**", NormalizedEmail = "*****@*****.**", EmailConfirmed = true, LockoutEnabled = false, SecurityStamp = Guid.NewGuid().ToString() }; var role = new IdentityRole { Name = "User", NormalizedName = "User" }; roleStore.CreateAsync(role); if (!context.Users.Any(u => u.UserName == user.UserName)) { var hasher = new PasswordHasher <ApplicationUser>(); var hashed = hasher.HashPassword(user, password); user.PasswordHash = hashed; var userStore = new UserStore <ApplicationUser>(context); userStore.CreateAsync(user); } context.SaveChangesAsync(); // after the user and roles are inserted... // this has to work.. context.UserRoles.Add(new IdentityUserRole <string> { RoleId = role.Id, UserId = user.Id }); // save again. context.SaveChanges(); this.userId = user.Id; } }
public void Create() { new DefaultEditionCreator(_context).Create(); new DefaultLanguagesCreator(_context).Create(); new HostRoleAndUserCreator(_context).Create(); new DefaultSettingsCreator(_context).Create(); _context.SaveChanges(); }
private void AddSettingIfNotExists(string name, string value, int?tenantId = null) { if (_context.Settings.IgnoreQueryFilters().Any(s => s.Name == name && s.TenantId == tenantId && s.UserId == null)) { return; } _context.Settings.Add(new Setting(tenantId, null, name, value)); _context.SaveChanges(); }
private void AddLanguageIfNotExists(ApplicationLanguage language) { if (_context.Languages.IgnoreQueryFilters().Any(l => l.TenantId == language.TenantId && l.Name == language.Name)) { return; } _context.Languages.Add(language); _context.SaveChanges(); }
internal static TemplateDbContext CreateInMemoryContext(string name = "default") { var optionsBuilder = new DbContextOptionsBuilder <TemplateDbContext>(); optionsBuilder.UseInMemoryDatabase(name); var context = new TemplateDbContext(optionsBuilder.Options); context.NameAddresses.AddRange(CreateNameAddresseses()); context.SaveChanges(); return(context); }
private void CreateDefaultTenant() { // Default tenant var defaultTenant = _context.Tenants.IgnoreQueryFilters().FirstOrDefault(t => t.TenancyName == AbpTenantBase.DefaultTenantName); if (defaultTenant == null) { defaultTenant = new Tenant(AbpTenantBase.DefaultTenantName, AbpTenantBase.DefaultTenantName); var defaultEdition = _context.Editions.IgnoreQueryFilters().FirstOrDefault(e => e.Name == EditionManager.DefaultEditionName); if (defaultEdition != null) { defaultTenant.EditionId = defaultEdition.Id; } _context.Tenants.Add(defaultTenant); _context.SaveChanges(); } }
private void CreateHostRoleAndUsers() { // Admin role for host var adminRoleForHost = _context.Roles.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == null && r.Name == StaticRoleNames.Host.Admin); if (adminRoleForHost == null) { adminRoleForHost = _context.Roles.Add(new Role(null, StaticRoleNames.Host.Admin, StaticRoleNames.Host.Admin) { IsStatic = true, IsDefault = true }).Entity; _context.SaveChanges(); } // Grant all permissions to admin role for host var grantedPermissions = _context.Permissions.IgnoreQueryFilters() .OfType <RolePermissionSetting>() .Where(p => p.TenantId == null && p.RoleId == adminRoleForHost.Id) .Select(p => p.Name) .ToList(); var permissions = PermissionFinder .GetAllPermissions(new TemplateAuthorizationProvider()) .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Host) && !grantedPermissions.Contains(p.Name)) .ToList(); if (permissions.Any()) { _context.Permissions.AddRange( permissions.Select(permission => new RolePermissionSetting { TenantId = null, Name = permission.Name, IsGranted = true, RoleId = adminRoleForHost.Id }) ); _context.SaveChanges(); } // Admin user for host var adminUserForHost = _context.Users.IgnoreQueryFilters().FirstOrDefault(u => u.TenantId == null && u.UserName == AbpUserBase.AdminUserName); if (adminUserForHost == null) { var user = new User { TenantId = null, UserName = AbpUserBase.AdminUserName, Name = "admin", Surname = "admin", EmailAddress = "*****@*****.**", IsEmailConfirmed = true, IsActive = true }; user.Password = new PasswordHasher <User>(new OptionsWrapper <PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(user, "123qwe"); user.SetNormalizedNames(); adminUserForHost = _context.Users.Add(user).Entity; _context.SaveChanges(); // Assign Admin role to admin user _context.UserRoles.Add(new UserRole(null, adminUserForHost.Id, adminRoleForHost.Id)); _context.SaveChanges(); _context.SaveChanges(); } }
private void CreateRolesAndUsers() { // Admin role var adminRole = _context.Roles.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.Admin); if (adminRole == null) { adminRole = _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.Admin, StaticRoleNames.Tenants.Admin) { IsStatic = true }).Entity; _context.SaveChanges(); } // Grant all permissions to admin role var grantedPermissions = _context.Permissions.IgnoreQueryFilters() .OfType <RolePermissionSetting>() .Where(p => p.TenantId == _tenantId && p.RoleId == adminRole.Id) .Select(p => p.Name) .ToList(); var permissions = PermissionFinder .GetAllPermissions(new TemplateAuthorizationProvider()) .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Tenant) && !grantedPermissions.Contains(p.Name)) .ToList(); if (permissions.Any()) { _context.Permissions.AddRange( permissions.Select(permission => new RolePermissionSetting { TenantId = _tenantId, Name = permission.Name, IsGranted = true, RoleId = adminRole.Id }) ); _context.SaveChanges(); } // Admin user var adminUser = _context.Users.IgnoreQueryFilters().FirstOrDefault(u => u.TenantId == _tenantId && u.UserName == AbpUserBase.AdminUserName); if (adminUser == null) { adminUser = User.CreateTenantAdminUser(_tenantId, "*****@*****.**"); adminUser.Password = new PasswordHasher <User>(new OptionsWrapper <PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(adminUser, "123qwe"); adminUser.IsEmailConfirmed = true; adminUser.IsActive = true; _context.Users.Add(adminUser); _context.SaveChanges(); // Assign Admin role to admin user _context.UserRoles.Add(new UserRole(_tenantId, adminUser.Id, adminRole.Id)); _context.SaveChanges(); // User account of admin user if (_tenantId == 1) { _context.UserAccounts.Add(new UserAccount { TenantId = _tenantId, UserId = adminUser.Id, UserName = AbpUserBase.AdminUserName, EmailAddress = adminUser.EmailAddress }); _context.SaveChanges(); } } }
public void Add <T>(T entity) where T : class { _context.Add(entity); _context.SaveChanges(); }
public static void Initialize(TemplateDbContext context) { context.Database.EnsureCreated(); // Look for any students. if (context.Users.Any()) { // DB has been seeded return; } var users = new User[] { new User { FirstName = "Carson", LastName = "Alexander", Email = "*****@*****.**", Phone = "+55 11 91234-5678", Password = "******", ProfileImage = "profiles/img/carson.alexander", Gender = Gender.Male, BirthDate = DateTime.Now.AddYears(-30).AddMonths(-1).AddDays(-12), AccessAttempts = 0, LastAccessDate = DateTime.Now, LastAcceptanceTermsDate = DateTime.Now.AddDays(-45), Blocked = false, AddedDate = DateTime.Now.AddDays(-45), ModifiedDate = DateTime.Now.AddDays(-45), DeletedDate = null, IPAddress = "127.0.0.1", Active = true }, new User { FirstName = "Meredith", LastName = "Alonso", Email = "*****@*****.**", Phone = "+55 11 93245-5678", Password = "******", ProfileImage = "profiles/img/meredith.alonso", Gender = Gender.Female, BirthDate = DateTime.Now.AddYears(-25).AddMonths(-6).AddDays(12), AccessAttempts = 0, LastAccessDate = DateTime.Now, LastAcceptanceTermsDate = DateTime.Now.AddDays(-25), Blocked = false, AddedDate = DateTime.Now.AddDays(-25), ModifiedDate = DateTime.Now.AddDays(-25), DeletedDate = DateTime.Now.AddDays(-45), IPAddress = "192.168.1.2", Active = false }, new User { FirstName = "Arturo", LastName = "Anand", Email = "*****@*****.**", Phone = "+55 11 94675-5678", Password = "******", ProfileImage = "profiles/img/arturo.anand", Gender = Gender.Male, BirthDate = DateTime.Now.AddYears(-23).AddMonths(-2).AddDays(-7), AccessAttempts = 3, LastAccessDate = DateTime.Now.AddDays(-10), LastAcceptanceTermsDate = DateTime.Now.AddDays(-15), Blocked = true, AddedDate = DateTime.Now.AddDays(-15), ModifiedDate = DateTime.Now.AddDays(-15), DeletedDate = null, IPAddress = "192.168.1.2", Active = true }, new User { FirstName = "Gytis", LastName = "Barzdukas", Email = "*****@*****.**", Phone = "+55 11 91145-5678", Password = "******", ProfileImage = "profiles/img/gytis.barzdukas", Gender = Gender.Female, BirthDate = DateTime.Now.AddYears(-20).AddMonths(-0).AddDays(5), AccessAttempts = 3, LastAccessDate = DateTime.Now.AddDays(-5), LastAcceptanceTermsDate = DateTime.Now.AddDays(-5), Blocked = true, AddedDate = DateTime.Now.AddDays(-5), ModifiedDate = DateTime.Now.AddDays(-5), DeletedDate = null, IPAddress = "192.168.1.123", Active = true }, new User { FirstName = "Yan", LastName = "Li", Email = "*****@*****.**", Phone = "+55 11 99945-5678", Password = "******", ProfileImage = "profiles/img/yan.li", Gender = Gender.Male, BirthDate = DateTime.Now.AddYears(-20).AddMonths(-3).AddDays(4), AccessAttempts = 3, LastAccessDate = DateTime.Now, LastAcceptanceTermsDate = DateTime.Now.AddDays(-4), Blocked = true, AddedDate = DateTime.Now.AddDays(-4), ModifiedDate = DateTime.Now.AddDays(-4), DeletedDate = DateTime.Now.AddDays(-90), IPAddress = "192.168.1.345", Active = false }, new User { FirstName = "Peggy", LastName = "Justice", Email = "*****@*****.**", Phone = "+55 11 97777-5678", Password = "******", ProfileImage = "profiles/img/peggy.justice", Gender = Gender.Female, BirthDate = DateTime.Now.AddYears(-22).AddMonths(-9).AddDays(-25), AccessAttempts = 0, LastAccessDate = DateTime.Now, LastAcceptanceTermsDate = DateTime.Now.AddDays(-46), Blocked = false, AddedDate = DateTime.Now.AddDays(-46), ModifiedDate = DateTime.Now.AddDays(-46), DeletedDate = null, IPAddress = "192.168.1.762", Active = true }, new User { FirstName = "Laura", LastName = "Norman", Email = "*****@*****.**", Phone = "+55 11 92342-5678", Password = "******", ProfileImage = "profiles/img/laura.norman", Gender = Gender.Female, BirthDate = DateTime.Now.AddYears(-20).AddMonths(-8).AddDays(-1), AccessAttempts = 0, LastAccessDate = DateTime.Now.AddDays(-1), LastAcceptanceTermsDate = DateTime.Now.AddDays(-49), Blocked = false, AddedDate = DateTime.Now.AddDays(-49), ModifiedDate = DateTime.Now.AddDays(-49), DeletedDate = null, IPAddress = "192.168.1.23423", Active = true }, new User { FirstName = "Nino", LastName = "Olivetto", Email = "*****@*****.**", Phone = "+55 11 97867-5678", Password = "******", ProfileImage = "profiles/img/nino.olivetto", Gender = Gender.Male, BirthDate = DateTime.Now.AddYears(-24).AddMonths(-7).AddDays(-9), AccessAttempts = 0, LastAccessDate = DateTime.Now, LastAcceptanceTermsDate = DateTime.Now.AddDays(-40), Blocked = false, AddedDate = DateTime.Now.AddDays(-40), ModifiedDate = DateTime.Now.AddDays(-40), DeletedDate = null, IPAddress = "192.168.143.24565", Active = true } }; foreach (User s in users) { context.Users.Add(s); } context.SaveChanges(); ////var courses = new Course[] ////{ ////new Course{CourseID=1050,Title="Chemistry",Credits=3,}, ////new Course{CourseID=4022,Title="Microeconomics",Credits=3,}, ////new Course{CourseID=4041,Title="Macroeconomics",Credits=3,}, ////new Course{CourseID=1045,Title="Calculus",Credits=4,}, ////new Course{CourseID=3141,Title="Trigonometry",Credits=4,}, ////new Course{CourseID=2021,Title="Composition",Credits=3,}, ////new Course{CourseID=2042,Title="Literature",Credits=4,} ////}; ////foreach (Course c in courses) ////{ //// context.Courses.Add(c); ////} ////context.SaveChanges(); ////var enrollments = new Enrollment[] ////{ ////new Enrollment{StudentID=1,CourseID=1050,Grade=Grade.A}, ////new Enrollment{StudentID=1,CourseID=4022,Grade=Grade.C}, ////new Enrollment{StudentID=1,CourseID=4041,Grade=Grade.B}, ////new Enrollment{StudentID=2,CourseID=1045,Grade=Grade.B}, ////new Enrollment{StudentID=2,CourseID=3141,Grade=Grade.F}, ////new Enrollment{StudentID=2,CourseID=2021,Grade=Grade.F}, ////new Enrollment{StudentID=3,CourseID=1050}, ////new Enrollment{StudentID=4,CourseID=1050,}, ////new Enrollment{StudentID=4,CourseID=4022,Grade=Grade.F}, ////new Enrollment{StudentID=5,CourseID=4041,Grade=Grade.C}, ////new Enrollment{StudentID=6,CourseID=1045}, ////new Enrollment{StudentID=7,CourseID=3141,Grade=Grade.A}, ////}; ////foreach (Enrollment e in enrollments) ////{ //// context.Enrollments.Add(e); ////} ////context.SaveChanges(); }