从c#实体框架MVC创建表的异常
本文关键字:异常 创建 MVC 实体 框架 | 更新日期: 2023-09-27 18:17:39
public class SalesERPDAL:DbContext
{
public DbSet<Employee> Employees;
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>().ToTable("TblEmployee");
base.OnModelCreating(modelBuilder);
}
}
public class EmployeeBusinessLayer
{
public List<Employee> GetEmployees()
{
SalesERPDAL salesDalObj = new SalesERPDAL();
return salesDalObj.Employees.ToList(); **//Getting error on this call**
}
}
public class Employee
{
[Key]
public int EmployeeId { get; set; }
public string FName { get; set; }
public string LName { get; set; }
public int Salary { get; set; }
}
webconfig连接字符串:
<connectionStrings>
<add name="SalesERPDAL" connectionString="Data Source=CSCINDAI406933''SQLEXPRESS;Initial Catalog=SalesERPDB;Integrated Security=True;"></add>
尝试从"SalesERPDAL"类创建一个表"tbleemployee",如上所述。但是我从EmployeeBusinessLayer类的GetEmployees()中调用"salesDalObj.Employees.ToList()"时会出现运行时错误。
异常详细信息:参数Null异常未被用户代码处理:类型为"System"的异常。在System.Core.dll中发生了ArgumentNullException,但没有在用户代码中处理附加信息:值不能为空
我能够从不同的应用程序连接到这个数据库。我是新的实体框架,不知道为什么应用程序是打破。我将非常感激你的帮助。提前感谢。
您需要将连接字符串名称传递给DbContext
public class SalesERPDAL: DbContext
{
public SalesERPDAL() : base("connectionStringName") { }
public DbSet<Employee> Employees { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>().ToTable("TblEmployee");
base.OnModelCreating(modelBuilder);
}
}