I got this code from where it was working properly. The Department class and ApplicationUser class has one to many relationship. I am trying to seed data. The roles are being added to the database , but not the user or the department. This is the ApplicationUser class
public class ApplicationUser : IdentityUser<int> { //n-1 relationship public int DepartmentId { get; set; } public Department Department { get; set; } [DisplayName("First Name")] [Required, MaxLength(50, ErrorMessage = "Name cannot exceed 50 characters")] public string FirstName { get; set; } [DisplayName("Middle Name")] public string MiddleName { get; set; } [DisplayName("Last Name")] [Required] public string LastName { get; set; } } This is the department class
public class Department : Entity { public string Name { get; set; } public ICollection<ApplicationUser> Employees { get; set; } } This is the Context class
public class ManageContext : IdentityDbContext<ApplicationUser,IdentityRole<int>,int> { public ManageContext(DbContextOptions<ManageContext> dbContextOptions) :base(dbContextOptions) { } public DbSet<Department> Departments { get; set; } //the name of the table will be same as the name of the class private static void SetTableNamesAsSingle(ModelBuilder builder) { // Use the entity name instead of the Context.DbSet<T> name foreach (var entityType in builder.Model.GetEntityTypes()) { builder.Entity(entityType.ClrType).ToTable(entityType.ClrType.Name); } } protected override void OnModelCreating(ModelBuilder builder) { SetTableNamesAsSingle(builder); base.OnModelCreating(builder); } } There are 2 separate classes to seed the data. These have static methods in them to seed and create roles and users. This is the dataseeder class which is being used to seed the department class
public class DataSeeder { public static async Task SeedAsync(ManageContext manageContext, ILoggerFactory loggerFactory, int? retry = 0) { int retryForAvailability = retry.Value; try { await SeedDepartmentAsync(manageContext); } catch (Exception exception) { if (retryForAvailability < 10) { retryForAvailability++; var log = loggerFactory.CreateLogger<ManageContext>(); log.LogError(exception.Message); await SeedAsync (manageContext, loggerFactory, retryForAvailability); } throw; } } private static async Task SeedDepartmentAsync(ManageContext manageContext) { if (manageContext.Departments.Any()) return; var department = new List<Department>() { new Department() { Name ="HR"}, new Department() { Name = "IT"} }; manageContext.Departments.AddRange(department); await manageContext.SaveChangesAsync(); } } This the identityseeder class which is creating the roles and the user and populating the database
public class IdentitySeeder { #region Public Methods public static void Seed( ManageContext manageContext, RoleManager<IdentityRole<int>> roleManager, UserManager<ApplicationUser> userManager ) { // Create default Users (if there are none) if (!manageContext.Users.Any()) { CreateUsers(manageContext, roleManager, userManager) .GetAwaiter() .GetResult(); } }
没有评论:
发表评论