Roger Far
Roger Far

Reputation: 2385

Separating DbContext in multiple projects

I created a company MVC4 "template" which contains basic helpers, user functions etc, just stuff that is needed in every project.

Now I am trying to separate the Users part which consists of 3 parts: controllers/models/views, a business layer and a data layer.

Now I ran into the problem that I pulled out the data layer part and put it in a seperate project which will become a private Nuget package.

But the package won't know about the DbContext because it's in the DataLayer from the actual project website.

The DbContext for the website currently looks like this:

public class DataContext : DbContext
{
    ....
    public DbSet<Language> Languages { get; set; }
    public DbSet<User> Users { get; set; }
    ....
    public DbSet<Reservation> Reservations { get; set; }
    ....
}

The Languages and Users will go in a separate project/NuGet package and Reservations will remain on the website as it is site specific.

The Languages and Users tables have no reference to each other, but Reservations has a reference to both User and to Language.

What are the options here to still keep a full Code-First model including migrations. Could I create multiple DbContext for every project/NuGet separately, will code-first migrations be able to pick this up, or is there a better way?

Upvotes: 0

Views: 737

Answers (1)

Woland
Woland

Reputation: 2919

If your Reservations contains links to Language and User classes then you will have to use the same Context.

If you want more loosely coupled implementation, then just include IDs instead of actual classes, then you can use separate contexts, but you will loose the ease of referencing objects and so on. The implementation really depends on how big your project is and how you plan on updating the solution.

Upvotes: 1

Related Questions