EntityFramework 6 Mysql .NET Connector 6.9.9代码首次迁移

时间:2016-11-01 22:57:05

标签: c# mysql asp.net-mvc-5 entity-framework-6 ef-migrations

我有一个包含2个项目的解决方案,一个asp.net核心mvc 1.0.1和一个带有EF6的.NET 4.6.1类库,它包含我的实体,域模型,抽象和视图模型。我在我的asp.net核心项目中添加了类库作为参考。我将我的类库项目设置为我的启动项目并启用迁移甚至成功添加。但是一旦我做了Update-Database,我得到一个包含没有匹配元素的错误序列。所以我切换并创建了相同的项目,但没有拆分成类库和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);
    }
}

任何人都可以指导我修复此错误需要做些什么!?

0 个答案:

没有答案