Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: The entity type Employee is not part of the model for the current context.

MY CODE:
C#
public ActionResult Details(int id)
        {
            EmployeeContext employeeContext = new EmployeeContext();
            Employee employee = employeeContext.Employees.Single(emp => emp.EmployeeId == id);
            return View(employee);
        }

===========================================
XML
namespace MVCDemo.Models
{
    public class EmployeeContext:DbContext
    {
        public DbSet<Employee> Employees { get;set; }
    }
}

==================================
<add name="EmployeeContext" connectionstring="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=XEVOKE-PC;initial catalog=Sample;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providername="System.Data.EntityClient">
=====================================
C#
namespace MVCDemo.Models
{
    [Table("tblEmployee")]
    public class Employee
    {
        public int EmployeeId { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string City { get; set; }
    }
}
Posted
Updated 4-Nov-14 18:46pm
v2
Comments
Nathan Minier 4-Nov-14 12:22pm    
Do you have a connection string named "EmployeeContext" in your web.config?

1 solution

C#
public ActionResult Details(int id)
        {
            EmployeeContext employeeContext = new EmployeeContext();
Employee employee = (Employee)(from emp in employeeContext.Members where emp.EmployeeId == id select emp).FirstOrDefault()
            return View(employee);
        }


Hope this will definitely of help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900