Entity Framework Code first with models in a different project

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

Question

Evening, I know this is probably fairly simple, but I must be doing it incorrectly.

I'm attempting to solve the problem using 3 (as of now) projects:

  1. Mobile CV App
  2. Models
  3. DAL (Entity Framework)

Because the models will be reusable across projects and we could want to switch out the DAL etc. without having to fiddle with the models etc., I want to keep the models in a different project/assembly from the UI and the DAL.

Anyway, let's get to the issue.

In my models project, I've generated a few classes, including:

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; }
}

After that, I made 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;
    }
}

The database has been built, but there are no tables in it. I enabled migrations on the DAL project, added a migration, and called it update-database.

Anyway, I know I'm being stupid; any assistance would be greatly appreciated.

Thanks

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

Accepted Answer

Clients In order for Entity Framework to recognize it, it must be a property.

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