Everything was great, I liked everything, but as soon as I added OnModelCreating, everything went wrong. Previously, every time the application was guaranteed to enter the migration, then to the Seed function during the launch, and the data entered in the Seed were entered into the database. But with the expansion of classes for the database, with the appearance in the context of the damned OnModelCreating, the application does not go into either the migration or the Seed. What could be the problem..? I sin on the fact that Identity may be involved in this, which also adds its game to the database

public class User : IdentityUser { public string Name { get; set; } public string Surname { get; set; } public string Nickname { get; set; } } public class Instructor : User { public virtual ICollection<Subject> Subjects { get; set; } public Instructor() { Subjects = new List<Subject>(); } } public class Student : User { } public class Subject { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Instructor> Instructors { get; set; } public Subject() { Instructors = new List<Instructor>(); } } public class Evaluation { public string StudentId { get; set; } public int SubjectId { get; set; } public virtual Student Student { get; set; } public virtual Subject Subject { get; set; } public int Score { get; set; } } public class ApplicationDbContext : IdentityDbContext<User> { public DbSet<Subject> Subjects { get; set; } public DbSet<Evaluation> Evaluations { get; set; } public ApplicationDbContext() : base("StudentsWebsiteDb") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Subject>() .HasMany(i => i.Instructors) .WithMany(s => s.Subjects) .Map(k => k.MapLeftKey("UserId") .MapRightKey("SubjectId") .ToTable("SubjectInstructor")); modelBuilder.Entity<Evaluation>().HasKey(e => new { e.StudentId, e.SubjectId }); base.OnModelCreating(modelBuilder); } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } } 

Seed Method:

 public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext> { protected override void Seed(ApplicationDbContext context) { ApplicationUserManager userManager = new ApplicationUserManager(new UserStore<User>(context)); RoleManager<IdentityRole> roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context)); Dictionary<User, string> students = new Dictionary<User, string>(); Dictionary<User, string> instructors = new Dictionary<User, string>(); List<Subject> subjects = new List<Subject>(); IdentityRole deanRole = new IdentityRole { Name = "dean" }; roleManager.Create(deanRole); IdentityRole instructorRole = new IdentityRole { Name = "instructor" }; roleManager.Create(instructorRole); IdentityRole studentRole = new IdentityRole { Name = "student" }; roleManager.Create(studentRole); User dean = new User { UserName = "yyyyyyy@mail.ru", Email = "yyyyyyyy@mail.ru", Name = "Ruslan", Surname = "Krivoshein", Nickname = "guitarhero" }; IdentityResult result = userManager.Create(dean, "qweqwe123!"); dean.Claims.Add(new IdentityUserClaim { ClaimType = "Nickname", ClaimValue = dean.Nickname }); if (result.Succeeded) { userManager.AddToRole(dean.Id, deanRole.Name); } instructors.Add(new Instructor { UserName = "norbertwiener@science.ru", Email = "norbertwiener@science.ru", Name = "Norbert", Surname = "Wiener", Nickname = "Mr.Wiener" }, "wiener"); instructors.Add(new Instructor { UserName = "claudeshannon@science.ru", Email = "claudeshannon@science.ru", Name = "Claude", Surname = "Shannon", Nickname = "Mr.Shannon" }, "shannon"); instructors.Add(new Instructor { UserName = "alanturing@science.ru", Email = "alanturing@science.ru", Name = "Alan", Surname = "Turing", Nickname = "Mr.Turing" }, "turing"); instructors.Add(new Instructor { UserName = "johnneumann@science.ru", Email = "johnneumann@science.ru", Name = "John", Surname = "Neumann", Nickname = "Mr.Neumann" }, "neumann"); instructors.Add(new Instructor { UserName = "andreykolmogorov@science.ru", Email = "andreykolmogorov@science.ru", Name = "Andrey", Surname = "Kolmogorov", Nickname = "Mr.Kolmogorov" }, "kolmogorov"); instructors.Add(new Instructor { UserName = "alberteinstein@science.ru", Email = "alberteinstein@science.ru", Name = "Albert", Surname = "Einstein", Nickname = "Mr.Einstein" }, "einstein"); foreach (KeyValuePair<User, string> instructor in instructors) { IdentityResult res = userManager.Create(instructor.Key, instructor.Value); instructor.Key.Claims.Add(new IdentityUserClaim { ClaimType = "Nickname", ClaimValue = instructor.Key.Nickname }); if (res.Succeeded) { userManager.AddToRole(instructor.Key.Id, instructorRole.Name); } } students.Add(new Student { UserName = "kirkrotorkrik@mail.ru", Email = "kirkrotorkrik@mail.ru", Name = "Kirill", Surname = "Krotov", Nickname = "Mole" }, "1krotkrot!"); students.Add(new Student { UserName = "nickkkyyeremin@mail.ru", Email = "nickkkyyeremin@mail.ru", Name = "Nikolay", Surname = "Yeremin", Nickname = "erema" }, "!qwE12"); students.Add(new Student { UserName = "fe0fan0v@bk.ru", Email = "fe0fan0v@bk.ru", Name = "Sergey", Surname = "Feofanov", Nickname = "DarkSide13" }, "ezzzy_0"); students.Add(new Student { UserName = "someoneb100@mail.ru", Email = "someoneb100@mail.ru", Name = "Andrey", Surname = "Petrov", Nickname = "SomeoneB" }, "ap1234!"); students.Add(new Student { UserName = "fyrfyrfyr1324@gmail.com", Email = "fyrfyrfyr1324@gmail.com", Name = "Alexandra", Surname = "Yeremina", Nickname = "CrazyFox" }, "foxy_777"); students.Add(new Student { UserName = "oskarsmile12@yandex.ru", Email = "oskarsmile12@yandex.ru", Name = "Oskar", Surname = "Steblev", Nickname = "The_Best" }, "x1x1x1!"); students.Add(new Student { UserName = "eagleandrew@mail.ru", Email = "eagleandrew@mail.ru", Name = "Andrey", Surname = "Orlov", Nickname = "TheEagle" }, "ea6le/"); students.Add(new Student { UserName = "katypitch@gmail.com", Email = "katypitch@gmail.com", Name = "Ekaterina", Surname = "Pitch", Nickname = "Pretty" }, "kexxx:*"); students.Add(new Student { UserName = "crazy07taly@mail.ru", Email = "crazy07taly@mail.ru", Name = "Stanislav", Surname = "Taly", Nickname = "HawkTaly" }, "h2so4)"); students.Add(new Student { UserName = "maximusalive666@mail.ru", Email = "maximusalive666@mail.ru", Name = "Maxim", Surname = "Zhitnev", Nickname = "Maximus" }, "max/max"); foreach (KeyValuePair<User, string> student in students) { IdentityResult res = userManager.Create(student.Key, student.Value); student.Key.Claims.Add(new IdentityUserClaim { ClaimType = "Nickname", ClaimValue = student.Key.Nickname }); if (res.Succeeded) { userManager.AddToRole(student.Key.Id, studentRole.Name); } } subjects.Add(new Subject { Id = 1, Name = "Physic" }); subjects.Add(new Subject { Id = 2, Name = "Math" }); subjects.Add(new Subject { Id = 3, Name = "Electrical and electronics" }); subjects.Add(new Subject { Id = 4, Name = "Theory of automata" }); subjects.Add(new Subject { Id = 5, Name = "Informatics" }); foreach (Subject subject in subjects) { context.Subjects.Add(subject); } base.Seed(context); } } 
  • As for me, you can do without OnModelCreating. And give the code Seed (). Identity has nothing to do with it, it just creates several tables in the entiti. - Nazar Kalytiuk
  • @ NazarKalituk, led. But I added only the creation of items. And the program doesn't enter this method at all for some reason ... - guitarhero
  • and even if it enters it, that the Subjects table is not full - guitarhero
  • it would be nice before base.Seed (context); write context.SaveChanges (); - Nazar Kalytiuk
  • In Global.asax you need to register Database.SetInitializer (new ApplicationDbInitializer ()); at the beginning of Application_Start (). I have enough so, although in the answer below is also not bad. - Nazar Kalytiuk

1 answer 1

At a foreign forum, I found this answer:

 Database.SetInitializer(new MyInitializer()); MyContext db = new MyContext(); db.Database.Initialize(true); 

I will no longer be afraid to go to him)