1

I have a web api project "Reactivities" and another project "Persisitence" to place my DbContext. When I try to use my add-migration in the Reactivities, using this syntax in the console add-migration InitalCreate -Context .\Persistence I get this error:

No DbContext named '.\Persistence' was found.

Even if I use add-migration InitialCreate -Context .\Persistence\DataContext still I get the same error,

This is in my startup class in Reactivities

services.AddDbContext<DataContext>(opt =>
            {
                opt.UseSqlite(Configuration.GetConnectionString("DefaultConnection"));
            });

How can I correct this?

2
  • at the top of the console window there is a combobox with all projects listed did you select the one that contains said context? Commented May 29, 2020 at 9:22
  • are you tried change selected project to Persistence in package manager console Commented May 29, 2020 at 9:24

1 Answer 1

1

You have to do migration work in a project that instantiates the dbcontext, in my case I have created a dbmigrator project with a class that inherits the IDesignTimeDbContextFactory<T> where T is the dbcontext type.

This allows me to have a project that has the dbcontexts, a project that works with migrations and a project that uses the dbcontexts independently of each other.

This guide shows you how to use the interface i mentioned: https://codingblast.com/entityframework-core-idesigntimedbcontextfactory/

Sign up to request clarification or add additional context in comments.

3 Comments

it is not mentioned that it has more than one dbcontex
no but the essential part is that to work with migrations, the target project has to be able to instantiate. if "Persisitence" cannot instantiate the dbcontext maybe because it does not have the settings or any other code than the definition, you can add a class that implements the IDesignTimeDbContextFactory<T> to that project and tell it the settings and that will allow you to use migrations with that project even if the project does not "use" the dbcontext for anything.
Also, if he were to add migrations targeting the Reactivities project, then the migrations would be added in the Reactivities project which kind of defeats the purpose of removing the dbcontext from that project in the first place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.