Add the ability to run migrations on DbContexts in referenced assemblies
See original GitHub issueAs far as I know, this is not currently possible. The use case is to create a modular system, where modules (in separate assemblies) each contribute one or more DbContexts to the main application. When you run migration add
on the main project (possibly with the --all
flag), it will include DbContexts in referenced assemblies.
I don’t know if this would scan assemblies or use the DI container (in ASP, at least). Perhaps both. The latter would provide access to connection strings configured in that way.
Related questions:
- Is this an appropriate way to supply the main application with additional entities, while still having a DbContext local to the module to use in services/repositories specific to that module? It will mean that there will be quite a few DbContexts (zero or more per module), but I don’t think that this could affect performance in any way, since they should be lazy-loaded via DI.
- I haven’t yet looked into this, but there will be times when I would need something like an
IdentityUser
in the module’s DbContext. For example, if it’s a forum module, it might have aTopic
entity with a relationship to an ApplicationUser/IdentityUser. Is it possible to add a DbSet to a DbContext using an interface maybe, where the concrete type is later discovered? Likepublic DbSet<IIdentityUser> User { get; set; }
(that interface doesn’t actually exist, it’s just an example). The same question would apply to adding the relationship to the entity itself.
Issue Analytics
- State:
- Created 8 years ago
- Comments:15 (9 by maintainers)
Top Results From Across the Web
Add the ability to run migrations on DbContexts in ...
When you run migration add on the main project (possibly with the --all flag), it will include DbContexts in referenced assemblies. I don't...
Read more >Add migration with different assembly
Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("Project.Api")) ...
Read more >Using a Separate Migrations Project - EF Core
Create a new class library. Add a reference to your DbContext project. Move the migrations and model snapshot files to the class library....
Read more >Migrations and Seed Data With Entity Framework Core
EF Core provides a method called Migrate to execute migration actions for us. All we have to do is to create model classes,...
Read more >Migrations with Multiple Providers - EF Core
One way to create multiple migration sets is to use one DbContext type per provider. ... Specify the context type when adding new...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@rowanmiller, Thanks, I will reply there.
@bricelam,
One may not have control of the project containing the DbContext. Imagine a “forum” module that you install with NuGet. You can’t run migrations there, because you don’t have the source, and regardless, it won’t produce the desired result, which is a combined migration in the application that included the module.
The second option that you mentioned will not work either.
What is required is the following:
services.AddEntityFramework().AddDbContext<TheModule.ForumDbContext)>()
or by passing the services container to the module during Startup and letting it add its own context (this is the way that I had planned to do it).dnx . ef migration add MyMigration --all
from the web application, and it creates a migration that includes all DbContexts (either by looking at contexts added via the services container, or by doing “assembly scanning”)This (along with #757) would be very powerful, and would help in enabling the creation of modular applications. Please keep it in mind when working on related issues.
I have no idea what work needs to be done for this issue anymore. 😃 I suspect most of it will be covered by #2294. Moving to a discussion and closing. Please create new issues for anything that you think is not already tracked elsewhere.