jasontaylordev / NorthwindTraders

Northwind Traders is a sample application built using ASP.NET Core and Entity Framework Core.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Infrastructure

danijel88 opened this issue · comments

When I run command add-migration Identity -p Infrastructure -c ApplicationDbContext I got next error:
Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

image

Can you help me to understand what is wrong ?

Regards,
Danijel

The answer is in your question. Please read the information on the URL you provided.
Migrations are design tools that need to construct a DbContext at design time, not at run time. For this to happen, you need to define methods for Migrations to build up a DbContext. I've done that using a factory:
https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dbcontext-creation#from-a-design-time-factory

@mlqmarkos12 You are partial right. I can not find now on microsoft site topic, but there is written if does not exist IDesignTimeDbContextFactory, system is searching configuraton in generic host. That is the reason why you have in program.cs set it path to connection string.

There are multiple ways of doing it. IDesignTimeDbContextFactory bypass all of them. If you prefer you can register your DbContext via application services instead on Startup like mentioned here:
https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dbcontext-creation#from-application-services
Take into consideration that you will need to add a new constructor in your DbContext that receives DbContextOptions as parameter:

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

Sorry for late replay, let's make easy. Please clone this project. Remove migration folder from Infrastructure and try to add migration. Let me know if you got an error ?
I had fixed locally code with adding IDesignTimeDbContextFactory but current project on git doesn't work that is my point.

If you look at Jasons 'CleanArchitecture' repo (https://github.com/JasonGT/CleanArchitecture). Persistence is part of the Infrastructure.

Did you try ?
I had clone this repo and try to add migration for Infrastructure and I got an error.

@danijel88 you need to update npm and node ... had same error.

npm and node for command add-migration which is command of EF ?
Can you explain me why that command depend on npm and node ?

Thank you for your interest in this project. This repository has been archived and is no longer actively maintained or supported. We appreciate your understanding. Feel free to explore the codebase and adapt it to your own needs if it serves as a useful reference. If you have any further questions or concerns, please refer to the README for more information.