I have a class library which contains all of my Entity Framework code.
I'm developing an solution that involves several sub-projects, including an ASP.NET MVC project. My model has been separated into a separate assembly because I need to use it from the various other projects in the solution.
I have divided my solution into three tier:
1) `DAL` (Data Access Layer Entity Framework .EDMX) DATABASE First approach...
2) `Service` layer (will be calling DAL to get data from db....)
3) `UI` (which will be MVC 4 framework)
So in my Service project I have reference of Data Access dll
in ASP.NET MVC Project I have reference of Service project
However, I am now starting to build a ASP.NET MVC.
I tried adding a controller and selecting Model class from my DAL received an error
Error 1 The type 'myproj_dal.requester' is defined in an assembly that is not referenced. You must add a reference to assembly 'myproj_dal, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. \issoa_ui\Controllers\RequesterController.cs
here is my controller looks like:
public ActionResult Index()
{
issoa_service.RequesterService reqService = new issoa_service.RequesterService();
var model = reqService.GetRequesters(); //**<<<ERROR**
return View(model);
}
In my Web.Config
I have connection string exactly that I have in my DAL app.config.
Here is my RequesterService
looks like this:
public class RequesterService
{
db_entities edmx = new db_entities();
public IList<Requester> GetRequesters()
{
IList<Requester> model = edmx.Requester.ToList();
return model;
}
}
Model.edmx
>>>>>Model.tt
>>>Requester.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace myproj_dal
{
using System;
using System.Collections.Generic;
public partial class Requester
{
public int Id { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
}
}
my question is:
1) Why I'm getting the above error and do i need to add reference of DAL to UI? if yes then i am defeating the whole purpose of loosely coupled.
2) What is the right way of doing; if I'm not doing based on the best standards.
Can anybody please point me or correct me or shout at me or redirect me to some kinda explanation or a sample project will help.?
If you don't want to reference your DAL from your web project, you need to take the entity models out of your DAL and put them in a separate project.
Create a project called MyProj.Entities
and reference this from everywhere that needs it (i.e. all of your other projects). Put Requester
and other entity classes inside this project.
Now your web project no longer has to be tightly coupled with your DAL but you can still share your entity classes between them.