6

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

4
  • 1
    i believe your Clients should be a property with { get; set; } instead of a field. i.e.: public DbSet<Client> Clients { get; set; } Commented Oct 27, 2014 at 19:39
  • HAHA, you know that bit where I said I'm sure I'm doing something dense?! Two of us looked at that code and didn't spot that... Its the simple and obvious things that are the hardest to spot! Thanks! Commented Oct 27, 2014 at 19:44
  • I'll repost as an answer Commented Oct 27, 2014 at 19:46
  • Connection string - Yeah, DLeh spotted my very simple mistake! With regards to migrations in dev, this is mainly a bit of a mess about app for me to get up to speed on code first & migrations, we cant use them in production as the DBAs wont allow it (Don't ask) and much of what we do uses Sybase12 which doesn't play nicely with EF so we cant really use it that much anyway. Thanks for the assistance though! Commented Oct 27, 2014 at 19:50

1 Answer 1

7

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

public class InvoiceDataContext : DbContext
{
    public DbSet<Client> Clients { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Told you I was being dense! Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.