I have built a MVC application using EF. The programs run fine. Now the code rund on the classes created by EF in the under the context files. I am trying to have a repository pattern for this.
context code
public partial class EmpDbEntities : DbContext
{
public EmpDbEntities()
: base("name=EmpDbEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Employee> Employees { get; set; }
public virtual DbSet<Department> Departments { get; set; }
}
Controller Code
public EmployeeController()
{
db = new EmpDbEntities();
}
// GET: Employee
public ActionResult Index()
{
var employees = db.Employees.Include("Department").ToList();
return View(employees);
}
View
@model IEnumerable<MVCTestWebApp.Employee>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.EmpName)
</th>
<th>
@Html.DisplayNameFor(model => model.Department.DeptName)
</th>
<th>
@Html.DisplayNameFor(model => model.JoiningDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Active)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.EmpName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Department.DeptName)
</td>
<td>
@Html.DisplayFor(modelItem => item.JoiningDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Active)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.EmpId }) |
@Html.ActionLink("Details", "Details", new { id=item.EmpId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.EmpId })
</td>
</tr>
}
</table>
What i am trying to do is have a Employees class and Department classes in Models. Also in Repository Folder to have seperate IRepository Interface with all the methods to save, update and delete. One concrete class to implement the interface. Finally to call it in the controller.
However when i try to refer the model classes, it throws an error - The entity type clsEmployee is not part of the model for the current context. I tried changing the edmx names but it was still throwing an error.