简体   繁体   English

实体框架-MVC 5异常

[英]Entity framework - MVC 5 Exception

System.InvalidOperationException occurred HResult=0x80131509 发生System.InvalidOperationException HResult = 0x80131509

Error 错误

Failed to set database initializer of type 'ClassEnrollment.DataAccess.SchoolInitializer,ClassEnrollment' for DbContext type 'ClassEnrollment.DataAccess.SchoolContext,ClassEnrollment' specified in the application configuration. 无法为应用程序配置中指定的DbContext类型“ ClassEnrollment.DataAccess.SchoolContext,ClassEnrollment”设置类型为“ ClassEnrollment.DataAccess.SchoolInitializer,ClassEnrollment”的数据库初始化程序。 See inner exception for details. 有关详细信息,请参见内部异常。 Source= StackTrace: Inner Exception 1: TypeLoadException: Could not load type 'ClassEnrollment.DataAccess.SchoolInitializer' from assembly 'ClassEnrollment'. Source = StackTrace:内部异常1:TypeLoadException:无法从程序集“ ClassEnrollment”中加载类型“ ClassEnrollment.DataAccess.SchoolInitializer”。

namespace ClassEnrollment.Models
{
    public class Student
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime EnrollemtnDate { get; set; }

        public virtual ICollection<Enrollment> Enrollments { get; set; }

    }
}
namespace ClassEnrollment.Models

{
    public class Enrollment {
    public int EnrollmentID{ get; set; }
    public int CourseID { get; set; }
    public int StudentID { get; set; }

    public Grade? Grade { get; set; }

    public Student Student { get; set; }
    public Course Course { get; set; }
}

    public enum Grade {
        A, B, C , D , F
    }
}



public class SchoolContext : DbContext
    {
        public SchoolContext() :base("SchoolContext") { }

    public DbSet<Student> Students { get; set; }
    public DbSet <Enrollment> Enrollments { get; set; }

    public DbSet <Course> Courses { get; set; }

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

namespace ClassEnrollment.Models
{
    public class SchoolInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<SchoolContext>
    {
        protected override void Seed(SchoolContext context)
        {
            var Students = new List<Student> {
                new Student{FirstName="Kamal",LastName="Ranasighe",EnrollemtnDate=DateTime.Parse("2017-09-01") },
                new Student{FirstName="Nimal",LastName="Chaturanga",EnrollemtnDate=DateTime.Parse("2017-09-02") },
                new Student{FirstName="Namal",LastName="Silva",EnrollemtnDate=DateTime.Parse("2017-09-11") }
            };

            Students.ForEach(s=> context.Students.Add(s));
            context.SaveChanges();

            var courses = new List<Course>
            {
                new Course{CourseID=1005,Title="Developing C# Application",Credits=5},
                new Course{CourseID=1015,Title="Developing Xamarin Application",Credits=5},
                new Course{CourseID=1025,Title="Developing ASP.NET Application",Credits=5}
            };

            courses.ForEach(c => context.Courses.Add(c));
            context.SaveChanges();

            var Enrollments = new List<Enrollment>
            {
                new Enrollment{StudentID=1, CourseID=1005,Grade=Grade.A},
                new Enrollment{StudentID=2, CourseID=1025,Grade=Grade.C},
                new Enrollment{StudentID=3, CourseID=1015,Grade=Grade.F}
            };

            Enrollments.ForEach(e=>context.Enrollments.Add(e));
            context.SaveChanges();
        }
    }
}

I figured the as to why the error. 我弄清楚了为什么会出错。 This is because web.config file couldn't find the initializer's location. 这是因为web.config文件找不到初始化程序的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM