I have spent some time getting my Data Access Layer (DAL) setup with Repositories and Entity Framework. The DAL is a separate project (Class library project) within the same Solution as my ASP.Net MVC 4 application. However I am not able to access the Database create from the ASP.Net MVC 4 application.
I get the following error
Cannot attach the file 'H:\Dropbox\TechCenter\Visual Studio 2012\Projects\TxValueCardProject\TxValueCardProject.Presentation\App_Data\TxValueCardProject.DataAccessLayer.TxDBContext.mdf' as database 'TxValueCardProject.DataAccessLayer.TxDBContext'.
In my DAL I have the POCO class:
public class Customer
{
public int CustomerId { get; set; }
public int RetailerId { get; set; }
public string CustomerName { get; set; }
public string CustomerEmail { get; set; }
public int PointsBalance { get; set; }
public decimal CashBalance { get; set; }
public ICollection<LoyaltyCard> LoyaltyCards { get; set; }
public virtual Retailer Retailer { get; set; }
}
And the DBContext Class
public class TxDBContext:DbContext
{
public DbSet<Customer> Customer { get; set; }
}
And this Repository
public class CustomerRepository : IRepository<Customer>
{
TxDBContext context = new TxDBContext();
public IQueryable<Customer> All
{
get { return context.Customers; }
}
}
With this setup, everything works fine in the DAL, all my Unit Test pass, and I can see the DB from SQL MS. Except when I switch to the ASP.Net MVC 4 project and appropriate references and Usings and everything build successfully and then I try a simple DB access like so, it fails
namespace TxValueCardProject.Presentation.Controllers
{
public class CustomerController : Controller
{
public ViewResult Index()
{
var custRepo = new CustomerRepository();
var customersInDb = custRepo.All;
return View(customersInDb);
}
The View is a simple loop statement @foreach (var item in Model)
Please help, what am I missing or doing wrong?