public async Task Add( IGeoCountry geoCountry, CancellationToken cancellationToken = default(CancellationToken) ) { ThrowIfDisposed(); cancellationToken.ThrowIfCancellationRequested(); if (geoCountry == null) { throw new ArgumentException("geoCountry must not be null"); } if (geoCountry.Id == Guid.Empty) { throw new ArgumentException("geoCountry must have a non-empty id"); } var country = GeoCountry.FromIGeoCountry(geoCountry); // convert from IGeoCountry dbContext.Countries.Add(country); int rowsAffected = await dbContext.SaveChangesAsync(cancellationToken) .ConfigureAwait(false); }
public async Task Create( ISiteSettings site, CancellationToken cancellationToken = default(CancellationToken)) { if (site == null) { throw new ArgumentException("site must not be null"); } if (site.Id == Guid.Empty) { throw new ArgumentException("site must have a non-empty Id"); } var siteSettings = SiteSettings.FromISiteSettings(site); dbContext.Sites.Add(siteSettings); int rowsAffected = await dbContext.SaveChangesAsync(cancellationToken) .ConfigureAwait(false); }
public async Task SaveAsync() { await context.SaveChangesAsync(); }
private static async Task EnsureData( ICoreDbContext db ) { int rowsAffected = 0; int count = await db.Countries.CountAsync <GeoCountry>(); if (count == 0) { foreach (GeoCountry c in InitialData.BuildCountryList()) { db.Countries.Add(c); } rowsAffected = await db.SaveChangesAsync(); } count = await db.States.CountAsync <GeoZone>(); if (count == 0) { foreach (GeoZone c in InitialData.BuildStateList()) { db.States.Add(c); } rowsAffected = await db.SaveChangesAsync(); } count = await db.Sites.CountAsync <SiteSettings>(); SiteSettings newSite = null; if (count == 0) { // create first site newSite = InitialData.BuildInitialSite(); db.Sites.Add(newSite); rowsAffected = await db.SaveChangesAsync(); } // ensure roles count = await db.Roles.CountAsync <SiteRole>(); if (count == 0) { var site = newSite; if (site == null) { site = await db.Sites.SingleOrDefaultAsync <SiteSettings>( s => s.Id != Guid.Empty && s.IsServerAdminSite == true); } if (site != null) { var adminRole = InitialData.BuildAdminRole(); adminRole.SiteId = site.Id; db.Roles.Add(adminRole); var roleAdminRole = InitialData.BuildRoleAdminRole(); roleAdminRole.SiteId = site.Id; db.Roles.Add(roleAdminRole); var contentAdminRole = InitialData.BuildContentAdminsRole(); contentAdminRole.SiteId = site.Id; db.Roles.Add(contentAdminRole); var authenticatedUserRole = InitialData.BuildAuthenticatedRole(); authenticatedUserRole.SiteId = site.Id; db.Roles.Add(authenticatedUserRole); rowsAffected = await db.SaveChangesAsync(); } } // ensure admin user count = await db.Users.CountAsync <SiteUser>(); if (count == 0) { SiteSettings site = await db.Sites.FirstOrDefaultAsync <SiteSettings>( s => s.Id != Guid.Empty && s.IsServerAdminSite == true); if (site != null) { var role = await db.Roles.FirstOrDefaultAsync( x => x.SiteId == site.Id && x.NormalizedRoleName == "ADMINISTRATORS"); if (role != null) { var adminUser = InitialData.BuildInitialAdmin(); adminUser.SiteId = site.Id; adminUser.Id = Guid.NewGuid(); adminUser.CanAutoLockout = false; db.Users.Add(adminUser); rowsAffected = await db.SaveChangesAsync(); if (rowsAffected > 0 && adminUser.Id != Guid.Empty) { var ur = new UserRole(); ur.RoleId = role.Id; ur.UserId = adminUser.Id; db.UserRoles.Add(ur); await db.SaveChangesAsync(); role = await db.Roles.SingleOrDefaultAsync( x => x.SiteId == site.Id && x.NormalizedRoleName == "Authenticated Users".ToUpperInvariant()); if (role != null) { ur = new UserRole(); ur.RoleId = role.Id; ur.UserId = adminUser.Id; db.UserRoles.Add(ur); await db.SaveChangesAsync(); } } } } } }
public async Task Create( ISiteUser user, CancellationToken cancellationToken = default(CancellationToken)) { ThrowIfDisposed(); cancellationToken.ThrowIfCancellationRequested(); if (user == null) { throw new ArgumentException("user can't be null"); } if (user.SiteId == Guid.Empty) { throw new ArgumentException("user must have a siteid"); } if (user.Id == Guid.Empty) { throw new ArgumentException("user must have a non empty guid for id"); } SiteUser siteUser = SiteUser.FromISiteUser(user); dbContext.Users.Add(siteUser); int rowsAffected = await dbContext.SaveChangesAsync(cancellationToken) .ConfigureAwait(false) ; //if (user.UserGuid == Guid.Empty) //{ // //user.UserId = siteUser.UserId; // user.UserGuid = siteUser.UserGuid; //} //return rowsAffected > 0; }
private static async Task EnsureData( ICoreDbContext db ) { int rowsAffected = 0; int count = await db.Countries.CountAsync<GeoCountry>(); if(count == 0) { foreach(GeoCountry c in InitialData.BuildCountryList()) { db.Countries.Add(c); } rowsAffected = await db.SaveChangesAsync(); } count = await db.States.CountAsync<GeoZone>(); if (count == 0) { foreach (GeoZone c in InitialData.BuildStateList()) { db.States.Add(c); } rowsAffected = await db.SaveChangesAsync(); } count = await db.Sites.CountAsync<SiteSettings>(); SiteSettings newSite = null; if (count == 0) { // create first site newSite = InitialData.BuildInitialSite(); db.Sites.Add(newSite); rowsAffected = await db.SaveChangesAsync(); } // ensure roles count = await db.Roles.CountAsync<SiteRole>(); if (count == 0) { var site = newSite; if(site == null) { site = await db.Sites.SingleOrDefaultAsync<SiteSettings>( s => s.Id != Guid.Empty && s.IsServerAdminSite == true); } if(site != null) { var adminRole = InitialData.BuildAdminRole(); adminRole.SiteId = site.Id; db.Roles.Add(adminRole); var roleAdminRole = InitialData.BuildRoleAdminRole(); roleAdminRole.SiteId = site.Id; db.Roles.Add(roleAdminRole); var contentAdminRole = InitialData.BuildContentAdminsRole(); contentAdminRole.SiteId = site.Id; db.Roles.Add(contentAdminRole); var authenticatedUserRole = InitialData.BuildAuthenticatedRole(); authenticatedUserRole.SiteId = site.Id; db.Roles.Add(authenticatedUserRole); rowsAffected = await db.SaveChangesAsync(); } } // ensure admin user count = await db.Users.CountAsync<SiteUser>(); if (count == 0) { SiteSettings site = await db.Sites.FirstOrDefaultAsync<SiteSettings>( s => s.Id != Guid.Empty && s.IsServerAdminSite == true); if (site != null) { var role = await db.Roles.FirstOrDefaultAsync( x => x.SiteId == site.Id && x.NormalizedRoleName == "ADMINISTRATORS"); if(role != null) { var adminUser = InitialData.BuildInitialAdmin(); adminUser.SiteId = site.Id; adminUser.Id = Guid.NewGuid(); db.Users.Add(adminUser); rowsAffected = await db.SaveChangesAsync(); if(rowsAffected > 0 && adminUser.Id != Guid.Empty) { var ur = new UserRole(); ur.RoleId = role.Id; ur.UserId = adminUser.Id; db.UserRoles.Add(ur); await db.SaveChangesAsync(); role = await db.Roles.SingleOrDefaultAsync( x => x.SiteId == site.Id && x.NormalizedRoleName == "Authenticated Users".ToUpperInvariant()); if(role != null) { ur = new UserRole(); ur.RoleId = role.Id; ur.UserId = adminUser.Id; db.UserRoles.Add(ur); await db.SaveChangesAsync(); } } } } } }