csharpfritz / Fritz.LiveStream

Comments, Questions, and Schedule for upcoming streams
GNU General Public License v3.0
85 stars 15 forks source link

Unable to run EF migrations after moving models and applicationdbcontext into a class library #68

Closed neelmallepally closed 6 years ago

neelmallepally commented 6 years ago

I have moved models and applicationdbcontext class into a separate class library project. Running dotnet ef migrations add MigrationName --project ProjName.Data.csproj produced an error message -

Unable to create an object of type 'ApplicationDbContext'. Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.

The link in the error message was useful. It says

Some of the EF Core Tools commands (for example, the Migrations commands) require a derived DbContext instance to be created at design time in order to gather details about the application's entity types and how they map to a database schema. In most cases, it is desirable that the DbContext thereby created is configured in a similar way to how it would be configured at run time.

The issue was fixed after creating a class deriving from IDesignTimeDbContextFactory in the class library project as suggested in the article. You didn't get this error message in the CoreWiki live stream when your moved DbContext to a class library or did I miss it? There is no class derived from IDesignTimeDbContextFactory in CoreWiki repo either.

It seems like I'm definitely missing something but not sure what it is. I do not have problem keeping that extra class in my class library project but scratching my head why this was not an issue in your live stream. @benrick did similar refactoring and he did not get that error message either. Any thoughts? Sorry, if I have not provided enough details.

I tried asp.net core 2.1.1 and 2.1.2 versions.

csharpfritz commented 6 years ago

We JUST did this on stream on Thursday the 12th. Code is here: https://github.com/csharpfritz/CoreWiki/tree/dev/CoreWiki.Data and video is here: https://www.youtube.com/watch?v=k-y6hCm-isI

csharpfritz commented 6 years ago

Break down your code... what is different between it and my implementation? Can you use my implementation without getting an error?