Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Code first with models in a different project

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

like image 745
D3vy Avatar asked Oct 27 '14 19:10

D3vy


1 Answers

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

public class InvoiceDataContext : DbContext
{
    public DbSet<Client> Clients { get; set; }
}
like image 128
DLeh Avatar answered Nov 15 '22 01:11

DLeh