0

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.

krishna_v
  • 1,501
  • 5
  • 30
  • 60
  • 4
    You don't need the Repository (Anti-)Pattern when you're using Entity Framework because your EF-provided `DbContext`-subclass class **is** your repository.. – Dai Jul 15 '17 at 11:23
  • 2
    Please read [here](https://stackoverflow.com/questions/14110890/not-using-repository-pattern-use-the-orm-as-is-ef) for more detailed information against using repositories with ORM – Alireza Jul 15 '17 at 11:24
  • @Dai, so if we are using EF, no need of separate Repository pattern. So Repository pattern is meaningful if we go for ADO.NET instead of EF? – krishna_v Jul 15 '17 at 11:27
  • @krishna_v Yes and no. Note that EF is built on-top of ADO.NET, so your question is meaningless and ignorant if taken at face-value. If you're asking about using ADO.NET primitives directly (such as `SqlConnection` and `SqlCommand`) then yes - the repository pattern can be a useful tool in those situations - but the entire industry has moved-on to adopt EF and other ORMs that render the Repository pattern obsolete. – Dai Jul 15 '17 at 11:33
  • @Dai yes, i was talking about ADO primitives - sqlConnection and Sqlcommand, Datareaders etc. – krishna_v Jul 15 '17 at 14:25
  • You have an error indicating that you offer some type not known to us as model type. I fail to see how using a repository pattern is related to solving this error. – Gert Arnold Jul 15 '17 at 19:12
  • @Gert Arnold, I have fixed it. I was just looking at different options to explore repository pattern. – krishna_v Jul 17 '17 at 13:09

0 Answers0