简体   繁体   English

从C#实体框架MVC创建表的异常

[英]Exception on creating a table from C# Entity framework MVC

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 connection string: webconfig连接字符串:

<connectionStrings>
<add name="SalesERPDAL" connectionString="Data Source=CSCINDAI406933\\SQLEXPRESS;Initial Catalog=SalesERPDB;Integrated Security=True;"></add>

Trying to create a table "TblEmployee" from "SalesERPDAL" class, as mentioned above. 如上所述,尝试从“ SalesERPDAL”类创建表“ TblEmployee”。 But I'm getting a runtime error on calling "salesDalObj.Employees.ToList();" 但是我在调​​用“ salesDalObj.Employees.ToList();”时遇到运行时错误。 from GetEmployees() from EmployeeBusinessLayer class. 来自EmployeeBusinessLayer类的GetEmployees()。

Exception details: Argument Null exception was unhandled by user code: An exception of type 'System.ArgumentNullException' occurred in System.Core.dll but was not handled in user code Additional information: Value cannot be null. 异常详细信息: 用户代码未处理Argument Null异常:System.Core.dll中发生了类型'System.ArgumentNullException'的异常,但未在用户代码中处理附加信息:值不能为null。

I'm able to connect to this DataBase from a different application. 我可以从其他应用程序连接到该数据库。 I'm new to Entity framework, not sure why the application is breaking. 我是实体框架的新手,不确定为什么应用程序损坏了。 Help would really be appreciated. 帮助将不胜感激。 Thanks in advance. 提前致谢。

You need to pass connection string name to DbContext 您需要将连接字符串名称传递给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);
    }
}

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

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