0
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:

<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. But I'm getting a runtime error on calling "salesDalObj.Employees.ToList();" from GetEmployees() from EmployeeBusinessLayer class.

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.

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.

4
  • Employees should be a property, not field Commented Nov 11, 2016 at 11:17
  • public DbSet<Employee> Employees { get; set; } should do it. Commented Nov 11, 2016 at 11:20
  • @vaxlt Thanks. But getting the below exception now, on the same method call. An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code Additional information: Instance failure. Commented Nov 11, 2016 at 11:34
  • @CemMutlu Thanks. But getting the below exception now, on the same method call. An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code Additional information: Instance failure. Commented Nov 11, 2016 at 11:34

1 Answer 1

1

You need to pass connection string name to 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);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Also you should dispose your salesDalObj after you're done. SalesERPDAL salesDalObj = new SalesERPDAL(); return salesDalObj.Employees.ToList(); //Getting error on this call

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.