System.InvalidOperationException occurred 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. See inner exception for details. Source= StackTrace: Inner Exception 1: TypeLoadException: Could not load type 'ClassEnrollment.DataAccess.SchoolInitializer' from assembly 'ClassEnrollment'.
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.
User contributions licensed under CC BY-SA 3.0