c# - 无法设置 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# - IIS 和 Visual Studio 中的问题
- jquery - mouseleave/mouseenter 和 mousemove 干扰,导致不需要的行为
- scala - 如何在 Spark Dataframe 的字符串中添加一列字符索引?
- powershell - SetEnvironmentVariable 来自 csv 单元格中保存的整数
- makefile - 指定编号的 Make -j(多个作业)在 QNX 中不起作用
- github - 在具有令牌访问权限的企业 github 上找不到 git 存储库?
- php - Eloquent 将 API 属性映射到字段名称
- r - keras 中带有神经网络的词嵌入
- python - aiohttp request.multipart() 从浏览器上传文件中一无所获
- visual-studio-code - 是否有切换面板而不是切换选项卡的快捷方式?