2

Background:

I have multiple classes called ApplicationDbContext. One is located in the root and the rest are divided up amongst their modularized area directories. Currently, I have the option of handling data migrations like so.


Question:

How would I rewrite each of my classes in the area folder's subdirectories to eliminate the individual migration calls?


Source:

Database Migration Commands Below


Establishing Migrations for Root Models

Enable-Migrations -ContextTypeName:JosephMCasey.Models.ApplicationDbContext -MigrationsDirectory:Migrations\Root
Add-Migration -configuration JosephMCasey.Migrations.Root.Configuration Root
Update-Database -configuration JosephMCasey.Migrations.Root.Configuration -Verbose

Establishing Migrations for each 'Areas' Model

Enable-Migrations -ContextTypeName:JosephMCasey.Areas.Article.Models.ApplicationDbContext -MigrationsDirectory:Migrations\Article
Add-Migration -configuration JosephMCasey.Migrations.Article.Configuration Article
Update-Database -configuration JosephMCasey.Migrations.Article.Configuration -Verbose

Idealized Migrations

Enable-Migrations
Add-Migration Application
Update-Database

C# Classes Below

Application\Models\IdentityModels.cs

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

Application\Areas\Article\Models\Article.cs

namespace JosephMCasey.Areas.Article.Models
{
    public class ArticleContent
    {
    ...
    }
    public class ApplicationDBContext : DbContext
    {
        public DbSet<ArticleContent> Articles { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Tag> Tags { get; set; }
    }
}

End Note

Am I just being too lazy or picky? Isn't it more effective to run a single context? I am new to this, so my understanding of best practices are shaky at best.


Resources for Guidance

Code First Migration in Multiple DbContext

Inheritance in Entity Framework: Table per Hierarchy

ASP.Net Forums

Package Manager Console Commands - get-help Enable-Migrations -detailed & get-help Add-Migration -detailed & get-help Update-Database -detailed

1 Answer 1

2

Each unique context must be migrated separately. There's no way around that. If you only want to do a single migration, you must combine all the contexts into a single context. Personally, I'd recommend doing that, anyways. There's no need for multiple contexts to serve a single app, unless some of them are for connecting to existing databases. In which case, you wouldn't need to migrate them anyways.

UPDATE

I'm still not sure how I can merge all of this to create a single context.

On a basic level, your single context would just need to reference all the various area model namespaces:

using AwesomeApp.Areas.Foo.Models;
using AwesomeApp.Areas.Bar.Models;
using AwesomeApp.Areas.Baz.Models;

public class AwesomeAppContext : DbContext
{
    // DbSets for each areas entities here
}

However, it would be preferable to simply reorganize your project. Entity classes don't have to go into a "Models" directory and they don't have to be stored along with the area that uses them. Personally, if I'm doing a relatively straight-forward MVC app, I usually reserve the main "Models" directory in the project root for all entities the application uses along with a single context that references each.

For more complicated applications, I move this outside of the project completely and have all my entities and my context live in a class library. I then migrate against the class library and merely add it as a reference to the project(s) that need those entities.

No matter what you do, though having entities strewn all about between different areas is going to make maintenance a bloody nightmare, as you'll always need to remember what entity goes with which area, and god forbid there's even any bleedover where you begin to use the same entity in multiple different areas but it's only defined in one.

Sign up to request clarification or add additional context in comments.

5 Comments

I know this is a very beginner question, but how would you write for Article.cs as an inheritance from IdentityModels.cs without placing them in the same file? Would you change namespace JosephMCasey.Areas.Article.Models to namespace JosephMCasey.Models, as reflected in IdentityModels.cs?
Files and their paths have nothing to do with the code inside. For ease, Visual Studio uses the path to construct a namespace when it generates a new class file, but the namespace can be whatever you want. However, even then, your classes need not be in the same namespace to make use of each other. You simply need to add a using statement to your code to include the other namespace, and you're off to the races.
So essentially you're saying namespaces are for scoping, and their naming is arbitrary. The using is for including the variables from one scope to another scope. Yet, I'm still not sure how I can merge all of this to create a single context. I'll post once I find out what I am doing wrong.
I wouldn't go so far as to say namespaces are "arbitrary", but yes, they're only meant for scoping. For example, in a class library for a product I work one, the actual .cs files are organized in hierarchies of directories in the solution, but every single class has a namespace in the form of Foo.Lib. As a result, including that once namespace lets you access anything in the class library, and the directories are only for organization within the solution.
Thanks for the answer. I definitely needed to know how my models needed to be stored. I tried finding other C# web projects on GitHub to see how they performed the task, but I couldn't find a directory clearly containing the models.

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.