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.

Migrations: Allow access to IServiceProvider

See original GitHub issue

Currently (as of version 2.0) when a migration is created with a non-empty constructor the following exception is thrown during migration:

System.MissingMethodException: No parameterless constructor defined for this object.

Since ConfigureServices is already called during migrations and the ServiceProvider is used to resolve the DbContext it would be really nice if the same ServiceProvider could be used to resolve the constructor of migrations.

This would make it possible to inject other services and run them during the migration.

My use-case:

We use ElasticSearch as a search index on top of some of our ef model types. When the model scheme is changed, the search index scheme also has to be updated and sometimes data has to be reindexed.

Previously we simply used to Migration Id to check if the search index is at the same scheme as the database. If the search index migration id was not the same as the one in the database we simply recreated the index and reindexed all the data.

Recently our data grow to a size that this is no longer feasonable. It would take way too long. So we only want to update the search index during migrations that would affect the parts of the scheme that are indexed.

So what I wanted to do is call the (for example) reindex method from an ef migration, when the search index should be updated. For this I would need the inject the ElasticSearch client into the migration, which is not supported atm. Hence the feature request.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ajcvickerscommented, Jan 17, 2018

We discussed this in triage and decided that we could make the service provider available to migrations via IInfrastructure. This unblocks people from doing advanced things while not promoting too much of a pit of failure.

1reaction
nphmullercommented, Jan 10, 2018

@ajcvickers I can see that. Didn’t really think about the script generation feature, since we don’t use it,. But that is a good point.

What’s documented here provided a good base for me to implement my use-case anyway. Since IMigrationsSqlGenerator supports DI. The only thing that was really confusing for me is that I should replace the internal service provider, instead of providing an implementation of IDesignTimeServies, since I though IMigrationsSqlGenerator was part of the design time service. But I had no previous experience with this part of EF, so that didn’t help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NET 6 how to run Migration automatically in program.cs
In .Net 5, we use to be able to call the migration by passing DataContext to Configure method and call the migration in...
Read more >
Migrate from ASP.NET Core 5.0 to 6.0
Some tools, such as EF migrations, use Program.CreateHostBuilder to access the app's IServiceProvider to execute custom logic in the context of ...
Read more >
Supporting EF Core migrations with WebApplicationBuilder
The EF Core tools use these events to run your application and to retrieve the IHost instance (and associated IServiceProvider ).
Read more >
Migrations in Entity Framework Core - .Net Core Central
Inside the Startup class, I will update the Configure method to accept IServiceProvider as the third parameter.
Read more >
EF Core App: Migrate on Startup
Here's a quick rundown on how to make an EF Core app migrate itself automatically ... If not, let's run the migrations and...
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