public static void Seed(ApplicationDbContext context) { if (context.Features.Count() != SecurityConfiguration.Features.Count()) { context.Features.RemoveRange(context.Features); var features = SecurityConfiguration.Features.Select(feat => Feature.Create(feat.Name)); context.Features.AddRange(features); context.SaveChanges(); } if (context.Roles.Count() != SecurityConfiguration.Roles.Count()) { context.Roles.RemoveRange(context.Roles); var roles = SecurityConfiguration.Roles .Select(r => Role.Create(r.Name)) .ToList(); context.Roles.AddRange(roles); foreach (var roleConfig in SecurityConfiguration.Roles) { var featureConfigs = SecurityConfiguration.Features.Where(f => f.Roles.Contains(roleConfig.Name)); var role = roles.Single(r => r.Name == roleConfig.Name); foreach (var feature in featureConfigs) { var roleFeature = RoleFeature.Create(role, context.Features.Single(x => x.Name == feature.Name)); role.AddRoleFeature(roleFeature); } } context.SaveChanges(); } }
public static void Seed(ApplicationDbContext context) { if (context.Features.Count() != SecurityConfiguration.Features.Count()) { context.Features.RemoveRange(context.Features); var features = SecurityConfiguration.Features.Select(feat => Feature.Create(feat.Name)); context.Features.AddRange(features); context.SaveChanges(); } if (context.Roles.Count() != SecurityConfiguration.Roles.Count()) { context.Roles.RemoveRange(context.Roles); var roles = SecurityConfiguration.Roles .Select(r => Role.Create(r.Name)) .ToList(); context.Roles.AddRange(roles); foreach (var roleConfig in SecurityConfiguration.Roles) { var featureConfigs = SecurityConfiguration.Features.Where(f => f.Roles.Contains(roleConfig.Name)); var role = roles.Single(r => r.Name == roleConfig.Name); foreach (var feature in featureConfigs) { var roleFeature = RoleFeature.Create(role, context.Features.Single(x => x.Name == feature.Name)); role.AddRoleFeature(roleFeature); //EF workarround to avoid DbUpdateConcurrencyException. //EF considered (bug?) the entity in modified state (n-n relationship) but it's a new object (Added) //https://docs.microsoft.com/fr-fr/ef/core/saving/concurrency //context.Entry(roleFeature).State = EntityState.Added; } } context.SaveChanges(); } }