微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

实体框架 6 - SqlException: 无效的对象名称“dbo.Course”

如何解决实体框架 6 - SqlException: 无效的对象名称“dbo.Course”

我试图创建一个不存在的数据库,但出现此错误

sqlException: 无效的对象名称 'dbo.Course'

错误发生在我的种子方法中的 SaveChanges 方法调用中:

protected override void Seed(CollegeDbContext context)
{
     _courses.ForEach(_ => context.Course.Add(_));
     _subjects.ForEach(_ => context.Subject.Add(_));
     _teachers.ForEach(_ => context.Teacher.Add(_));
     _students.ForEach(_ => context.Student.Add(_));

     context.SaveChanges();
}

我的初始化程序已使用 Application_Start()Database.Initializer调用

Database.Setinitializer(new CreateDatabaseIfNotExists<CollegeDbContext>());

它是这样实现的,它没有调用它的种子方法,所以我在构造函数调用

public class CollegeInitializer : CreateDatabaseIfNotExists<CollegeDbContext>
{
    private List<Student> _students;
    private List<Teacher> _teachers;
    private List<Subject> _subjects;
    private List<Course> _courses;
    private CollegeDbContext db;

    public CollegeInitializer()
    {
        db = new CollegeDbContext();

        if (db.Course == null) 
            return;

        _students = GetMorningStudents().Concat(GetEveningStudents()).ToList();
        _teachers = GetEveningTeachers().Concat(GetMorningTeachers()).ToList();
        _subjects = GetSubjects();
        _courses = GetCourses();
        
        SetStudentsSubjects();
        SetTeachersCourses();

        Seed(db);
    }
}

我什至在 OnModelCreating 方法中指定了表名。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    //base.OnModelCreating(modelBuilder);

    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

    var courseModel = modelBuilder.Entity<Course>();
    courseModel.HasKey(_ => _.CourseId);
    courseModel.HasMany(_ => _.Subjects).Withrequired(_ => _.Course);
    courseModel.HasMany(_ => _.Students).Withrequired(_ => _.Course).WillCascadeOnDelete(false);
    courseModel.HasMany(_ => _.Teachers).WithMany(_ => _.Courses);
    courseModel.ToTable("Course");

    var subjectModel = modelBuilder.Entity<Subject>();
    subjectModel.HasKey(_ => _.SubjectId);
    subjectModel.HasMany(_ => _.Students).WithMany(_ => _.Subjects);
    subjectModel.Hasrequired(_ => _.Teacher);
    subjectModel.Hasrequired(_ => _.Course).WithMany(_ => _.Subjects);
    subjectModel.ToTable("Subject");

    var studentModel = modelBuilder.Entity<Student>();
    studentModel.HasKey(_ => _.RegistrationNumberId);
    studentModel.HasMany(_ => _.Subjects).WithMany(_ => _.Students);
    studentModel.Hasrequired(_ => _.Course).WithMany(_ => _.Students).WillCascadeOnDelete(false);
    studentModel.Property(_ => _.Name).Isrequired().HasMaxLength(20);
    studentModel.ToTable("Student");
    //studentModel.Map(_ =>
    //{
    //    _.MapInheritedProperties();
    //});

    var teacherModel = modelBuilder.Entity<Teacher>();
    teacherModel.HasKey(_ => _.TeacherId);
    teacherModel.HasMany(_ => _.Courses).WithMany(m => m.Teachers);
    teacherModel.Property(_ => _.Name).Isrequired().HasMaxLength(20);
    teacherModel.ToTable("Teacher");
    //teacherModel.Map(_ =>
    //{
    //    _.MapInheritedProperties();
    //});
}

因此,如果使用的是应该创建的 IfDoesNotExist,我是否做错了?

解决方法

我删除了所有迁移信息然后程序开始使用初始化器

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。