EntityFramework 6 Mysql .NET Connector 6.9.9代码优先迁移

我有一个包含2个项目的解决方案,一个asp.net核心mvc 1.0.1和一个带有EF6的.NET 4.6.1类库,其中包含我的实体,领域模型,抽象和视图模型。 我在asp.net核心项目中添加了类库作为参考。 我将我的类库项目设置为我的启动项目,并启用迁移,甚至成功添加。 但是,一旦我做了更新数据库,我得到一个错误序列不包含匹配的元素。 所以我切换并创建了相同的项目,但没有拆分成类库和Web项目,现在只有一个asp.net mvc5项目,并且在那里,我甚至无法在启用迁移后添加迁移。 这两个计数我添加了mysql.data.entity 6.9.9,并遵循mysql本身和互联网上的其他丰富的指令的指示,但我仍然得到相同的错误。

这是我的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);
    }
}

任何人都可以指导我需要做什么来解决这个错误!?

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

上一篇: EntityFramework 6 Mysql .NET Connector 6.9.9 code first migrations

下一篇: EF using code first with existing table