Entity Framework Code first with models in a different project

asp.net-mvc c# entity-framework-6

Question

Evening, Im sure that this is probably quite simple... but Im doing something wrong.

I am trying to create a solution with 3 (for now) projects:

  1. MCV Web App
  2. Models
  3. DAL (Entity Framework)

I want to keep the models in a different project/assembly from the UI and the DAL because the models will be reusable between projects and we may want to swap out the DAL etc without having to mess about with the models etc...

Anyway, onto the problem.

I have created a few classes in my models project like :

public class Client
{
    public int ClientID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string PhoneNumber { get; set; }
    public string Notes { get; set; }
    public virtual Contact BillingContact { get; set; }
    public virtual ICollection<Contact> Contacts { get; set; }

    public virtual ICollection<Invoice> Invoices { get; set; }
}

I have then created a DBContext in the DAL

using System.Data.Entity;
using DBDS.Invoice.Models;

namespace DBDS.Invoice.DAL.EF
{
    public class InvoiceDataContext : DbContext
    {
        public DbSet<Client> Clients;
    }
}

I have enabled migrations on the DAL project, Added a migration and called update-database - the database has been created but with NO tables.

Anyway, Im sure Im being dense - any help will be appreciated.

Thanks

1
4
10/27/2014 7:38:32 PM

Accepted Answer

Clients needs to be a property for Entity Framework to pick it up

public class InvoiceDataContext : DbContext
{
    public DbSet<Client> Clients { get; set; }
}
6
10/27/2014 7:47:12 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow