EntityFramework 6 Mysql .NET Connector 6.9.9 code first migrations

I have a solution with 2 projects in it, an asp.net core mvc 1.0.1 and a .NET 4.6.1 class library with EF6 which contains my entities, domain models, abstractions and viewmodels. I have added the class library as a reference in my asp.net core project. I set my class library project as my startup project and enable migrations and even add successfully. But once I do Update-Database, I get an error sequence contains no matching elements. so I switch and create the same project but not split into class library and web project, its now one asp.net mvc5 project and there, I can't even add migrations after enabling migrations. Both counts I added mysql.data.entity 6.9.9 and followed the instructions at mysql itself and other rich instructions on the internet but I still get the same error.

Here is my dbcontext:

public class SchoolDb : IdentityDbContext<ApplicationUser>
{
    public static string ConnString { get; set; } =
        "Server=localhost; Database=schooldb; Uid=root; Pwd=l1nux43va;";
    public SchoolDb() : base("SchoolDb")
    {

    }

    public static SchoolDb Create()
    {
        return new SchoolDb();
    }

    public DbSet<Attendance> Attendance { get; set; }
    public DbSet<Class> Classes { get; set; }
    public DbSet<Student> Students { get; set; }
    public DbSet<Result> Results { get; set; }
    public DbSet<Teacher> Teachers { get; set; }
    public DbSet<MedicalHistory> MedicalHistories { get; set; }
    public DbSet<School> Schools { get; set; }
    public DbSet<Subject> Subjects { get; set; }
    public DbSet<Payment> Payments { get; set; }
    public DbSet<SchoolSession> Sessions { get; set; }
    public DbSet<TermWork> TermWorks { get; set; }
    public DbSet<Staff> Staff { get; set; }
    public DbSet<ScoreGrade> ScoreGrades { get; set; }
    public DbSet<LessonPlan> LessonPlans { get; set; }
    public DbSet<Hostel> Hostels { get; set; }
    public DbSet<Guardian> Guardians { get; set; }
    public DbSet<BehaviourActivity> BehaviourActivities { get; set; }
    public DbSet<Rating> Ratings { get; set; }
    public DbSet<TermWorkType> TermWorkTypes { get; set; }
    public DbSet<EntranceExam> EntranceExam { get; set; }
    public DbSet<EntranceExamCandidate> EntranceExamCandidate { get; set; }
    public DbSet<EntranceExamSubject> EntranceExamSubject { get; set; }




    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        //fluent annotations
        modelBuilder.Entity<Attendance>()
                    .HasMany(a => a.Students);
        modelBuilder.Entity<Attendance>()
                    .HasRequired(a => a.FormTeacher);
        modelBuilder.Entity<BehaviourActivity>()
                    .HasRequired(b => b.Rating)
                    .WithOptional(r => r.BehaviourActivity);

        modelBuilder.Entity<Class>()
                    .HasRequired(c => c.FormTeacher);
        modelBuilder.Entity<Class>()
                    .HasOptional(c => c.Subjects).WithOptionalPrincipal();
        modelBuilder.Entity<Club>()
                    .HasMany(c => c.Students);
        modelBuilder.Entity<EntranceExam>()
                    .HasMany(e => e.EntranceExamCandidate)
                    .WithRequired(e => e.EntranceExam).WillCascadeOnDelete(true);
        modelBuilder.Entity<EntranceExam>()
                    .HasMany(e => e.ExamSubjects)
                    .WithRequired(s => s.EntranceExam);
        modelBuilder.Entity<EntranceExamCandidate>()
                    .HasMany(e => e.ExamSubjects)
                    .WithRequired(s => s.EntranceExamCandidate);
        modelBuilder.Entity<Guardian>()
                    .HasMany(g => g.Students)
                    .WithRequired(s => s.Guardian);
        modelBuilder.Entity<Hostel>()
                    .HasMany(h => h.Students);
        modelBuilder.Entity<Hostel>()
                    .HasRequired(h => h.Patron);
        modelBuilder.Entity<MedicalHistory>()
                    .HasMany(m => m.Disabilities)
                    .WithRequired(d => d.MedicalHistory);
        modelBuilder.Entity<MedicalHistory>()
                    .HasMany(m => m.Illnesses)
                    .WithRequired(i => i.MedicalHistory);
        modelBuilder.Entity<MedicalHistory>()
                    .HasMany(m => m.Allergies)
                    .WithRequired(a => a.MedicalHistory);
        modelBuilder.Entity<Payment>()
                    .HasMany(p => p.PaymentTypes);
        modelBuilder.Entity<Payment>()
                    .HasRequired(p => p.SchoolSession);
        modelBuilder.Entity<Result>()
                    .HasRequired(r => r.Student);
        modelBuilder.Entity<Result>()
                    .HasRequired(r => r.SchoolSession);
        modelBuilder.Entity<Result>()
                    .HasRequired(r => r.Subject);
        modelBuilder.Entity<School>()
                    .HasMany(s => s.Students);
        modelBuilder.Entity<School>()
                    .HasMany(s => s.Teachers);
        modelBuilder.Entity<School>()
                    .HasMany(s => s.Staff)
                    .WithRequired(s => s.School);
        modelBuilder.Entity<SchoolSession>()
                    .HasRequired(ss => ss.School);
        modelBuilder.Entity<Student>()
                    .HasMany(s => s.Payments)
                    .WithRequired(p => p.Student);
        //modelBuilder.Entity<Student>()
        //            .HasMany(s => s.Subjects)
        //            .WithMany(s => s.Students)
        //            .Map(m =>
        //            {
        //                m.MapLeftKey("StudentId");
        //                m.MapRightKey("SubjectId");
        //                m.ToTable("StudentsSubjects");
        //            });
        modelBuilder.Entity<Student>()
                    .HasOptional(s => s.MedicalHistory)
                    .WithRequired(m => m.Student);
        modelBuilder.Entity<Student>()
                    .HasRequired(s => s.Class);
        modelBuilder.Entity<Subject>()
                    .HasOptional(s => s.ScoreGrade)
                    .WithOptionalPrincipal(sg => sg.Subject);
        modelBuilder.Entity<Teacher>()
                    .HasMany(t => t.LessonPlans)
                    .WithRequired(l => l.Teacher);
        //modelBuilder.Entity<Teacher>()
        //            .HasMany(t => t.Subjects)
        //            .WithMany(s => s.Teachers)
        //            .Map(m =>
        //            {
        //                m.MapLeftKey("TeacherId");
        //                m.MapRightKey("SubjectId");
        //                m.ToTable("TeachersSubjects");
        //            });
        modelBuilder.Entity<TermWork>()
                    .HasRequired(t => t.Student);
        modelBuilder.Entity<TermWork>()
                    .HasRequired(t => t.SchoolSession);
        modelBuilder.Entity<TermWork>()
                    .HasRequired(t => t.Subject);
        modelBuilder.Entity<TermWork>()
                    .HasRequired(t => t.TermWorkType)
                    .WithOptional(tt => tt.TermWork);
    }
}

can anyone guide me on what I need to do to fix this error!?

链接地址: http://www.djcxy.com/p/90290.html

上一篇: 首次迁移EntityFramework代码中的EntityValidationError

下一篇: EntityFramework 6 Mysql .NET Connector 6.9.9代码优先迁移