1

My approach to have connection string in config file and use database context visible for migration is using context factory, like here: Entity Framework Core migration - connection string. Building migration works, but when I try to run actual migration:

dotnet ef database update

I get error:

More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands.

For first I have no idea how it is possible to have several context since I have only one. Second problem, how to point out the factory (I cannot point out the context itself because it won't be configured properly, it is task of the factory).

As a temporary solution I executed migration from the C# code, but I would like to know how to do it from CLI (if this is possible at all with context factory).

Update 1: it looks like EF-tool is confused by its own output, because when I now have initial migration I can no longer execute creating another one (containing changes) -- I am getting the same error as above.

Update 2: using in factory my context type instead of generic DbContext helped for now, but since I already encountered building migration failed with empty migrations (while previously it worked), I am not yet sure this is really a solution.

astrowalker
  • 3,123
  • 3
  • 21
  • 40
  • I mean you must have more than class which inherits from `DbContext` – Vidmantas Blazevicius Oct 03 '19 at 07:56
  • @VidmantasBlazevicius, you are referring to the error? I have literally one class that inherits from `DbContext`, and I have in total 3 references to it -- my context (derivation), factory (which creates context) and migration files built by `dotnet ef migrations add Initial`. Nothing more. – astrowalker Oct 03 '19 at 08:34
  • Well, the error message says that there must be more than one context. Maybe within authorization code of asp.net core. Sometimes the scaffolding creates a new context there. If that's really not the case maybe deleting _/bin_ and _/obj_ folders can help. – Daniel Schmid Oct 03 '19 at 14:15
  • @DanielSchmid, this is no ASP.Net application :-) One project (plain library) with types and context, the other one -- for migration -- there is config file and factory for context. Today I even removed old migration and I still have this error while creating migration. – astrowalker Oct 04 '19 at 07:44

0 Answers0