question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add the ability to run migrations on DbContexts in referenced assemblies

See original GitHub issue

As 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:

  1. 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.
  2. 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 a Topic 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? Like public 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:closed
  • Created 8 years ago
  • Comments:15 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
glen-84commented, May 28, 2015

@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:

  1. I create a web application.
  2. I install a “module” (forum/blog/etc.) as a NuGet/DNX dependency.
  3. I add the DbContext from the module with 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).
  4. I run 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.

0reactions
bricelamcommented, Sep 23, 2015

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found