首页 > 解决方案 > 无法设置 ASP.NET 类型的数据库初始化程序

问题描述

我正在为一个学校项目制作一个 Code first 模型网站,但是每当我尝试加载学生页面时,我都会遇到此错误“无法设置类型的数据库初始化程序”

这是错误消息:

System.InvalidOperationException:“无法为应用程序配置中指定的 DbContext 类型“ASPNetWebApp_EF_CodeFirst_MVC.DAL.SMTIContext,ASPNetWebApp_EF_CodeFirst_MVC”设置类型为“ASPNetWebApp_EF_CodeFirst_MVC.DAL.SMTIInitializeData,ASPNetWebApp_EF_CodeFirst_MVC”的数据库初始化程序。有关详细信息,请参阅内部异常。TypeLoadException:无法从程序集“ASPNetWebApp_EF_CodeFirst_MVC”加载类型“ASPNetWebApp_EF_CodeFirst_MVC.DAL.SMTIContext”。

网络配置

<contexts>
          <context type="ASPNetWebApp_EF_CodeFirst_MVC.DAL.SMTIContext,ASPNetWebApp_EF_CodeFirst_MVC">
            <databaseInitializer type="ASPNetWebApp_EF_CodeFirst_MVC.DAL.SMTIInitializeData,ASPNetWebApp_EF_CodeFirst_MVC" />
          </context>
      </contexts>
<connectionStrings>
        <add name="SMTIContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=SMTI_DB;User Id=sa;Password=12345" providerName="System.Data.SqlClient"/>
    </connectionStrings>

我的数据库初始化程序类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ASPNetWebApp_EF_CodeFirst_MVC.Models.DAL
{
    public class SMTIInitializeData : System.Data.Entity.DropCreateDatabaseIfModelChanges<SMTIContext>
    {
        protected override void Seed(SMTIContext smtiContext)
        {
            var students = new List<Student> {
        new
        Student {
          StudentId = 12341, FirstName = "Mary", LastName = "Brown", Email = "mary@hotmail.com"
        },
        new
        Student {
          StudentId = 12342, FirstName = "Marie", LastName = "Brown", Email = "marie@yahoo.com"
        },
        new
        Student {
          StudentId = 12343, FirstName = "Thomas", LastName = "Moore", Email = "thomas@gmail.com"
        },
        new
        Student {
          StudentId = 12344, FirstName = "Jennifer", LastName = "Green", Email = "jennifer@yahoo.ca"
        },
        new
        Student {
          StudentId = 12345, FirstName = "Mary", LastName = "Green", Email = "maryGreen@yahoo.com"
        }
      };
            students.ForEach(s => smtiContext.Students.Add(s));
            smtiContext.SaveChanges();
            var projects = new List<Project> {
        new Project {
          ProjectCode = "PRJ101", ProjectTitle = "Hi-Tech Shopping Cart",
            DueDate = DateTime.Parse("4/5/2020")
        },
        new Project {
          ProjectCode = "PRJ102", ProjectTitle = "Property Rental App in Java",
            DueDate = DateTime.Parse("4/5/2020")
        },
        new Project {
          ProjectCode = "PRJ103", ProjectTitle = "Grade Tracking App in C++",
            DueDate = DateTime.Parse("4/5/2020")
        },
        new Project {
          ProjectCode = "PRJ104", ProjectTitle = "Hi-Tech Order Management in PHP",
            DueDate = DateTime.Parse("4/5/2020")
        },
        new Project {
          ProjectCode = "PRJ105", ProjectTitle = "Tic Tac Toe in C++",
            DueDate = DateTime.Parse("4/5/2020")
        },

      };
            projects.ForEach(p => smtiContext.Projects.Add(p));
            smtiContext.SaveChanges();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace ASPNetWebApp_EF_CodeFirst_MVC.Models.DAL
{
    public class SMTIContext : DbContext
    {
        public SMTIContext():base("SMTIContext") { }

        public DbSet<Student> Students { get; set; }
        public DbSet<Project> Projects { get; set; }
        public DbSet<ProjectAssignment> ProjectAssignments { get; set; }


    }
}

标签: c#asp.netentity-framework

解决方案


推荐阅读