0

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?

crthompson
  • 15,653
  • 6
  • 58
  • 80
Val Okafor
  • 3,371
  • 13
  • 47
  • 72
  • Maybe http://stackoverflow.com/questions/13275054/ef5-cannot-attach-the-file-0-as-database-1 can help you? – JLe Oct 04 '13 at 17:30
  • Thanks but I need those records, If I delete the DB, whats the point of going through and building out a Data Access Layer and populating it with records only to turn around and delete it. I will give it a try though. Thanks again. – Val Okafor Oct 04 '13 at 17:47
  • No, of course you shouldn't delete the database if it exists! I just though that if you had removed it from disk but not from the LocalDBb. – JLe Oct 04 '13 at 17:57
  • Thanks, it worked, but I am not happy that I lost all my record. Whats the point of following Layered Architecture if you are going to loose your data when you switch to another layer. – Val Okafor Oct 04 '13 at 17:59
  • Seems wierd, must have been some temporary problem with the database itself. You should be able to connect to it from whatever layer you'd like... – JLe Oct 04 '13 at 18:01

0 Answers0