NorthwindTraders icon indicating copy to clipboard operation
NorthwindTraders copied to clipboard

Infrastructure

Open danijel88 opened this issue 5 years ago • 8 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

danijel88 avatar Nov 05 '19 13:11 danijel88

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

mdelafuq-zz avatar Nov 07 '19 20:11 mdelafuq-zz

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

danijel88 avatar Nov 08 '19 07:11 danijel88

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)
    {
    }

mdelafuq-zz avatar Nov 08 '19 15:11 mdelafuq-zz

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.

danijel88 avatar Nov 14 '19 07:11 danijel88

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

mattijsing avatar Nov 14 '19 09:11 mattijsing

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

danijel88 avatar Nov 14 '19 09:11 danijel88

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

mattijsing avatar Nov 14 '19 09:11 mattijsing

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

danijel88 avatar Nov 14 '19 09:11 danijel88

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.

jasontaylordev avatar Jul 01 '23 22:07 jasontaylordev