Entity Framework Core 2 context and model in separate project to main project

entity-framework entity-framework-core

Question

How do i add-migrations into a separate data project??

I've got two projects, a data project with the context and models in and the website project which will use it. I've linked the two together all fine and dandy, the problem is that when I try and add a migration to the data project with add-migration initialmigration, the error

Unable to create an object of type 'myContext'. 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 has details on how to add the IDesignTimeDbContextFactory but which doesn't work because UseSqlServer doesn't exist as a command. That page does link to the updated version at https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/#update-main-method-in-programcs which says to put code into program.cs Even if I follow the instructions and add it to my data project (along with the associated references) so that I include

    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
    }

which is what it says to do, but I get the same error message. So, regardless of whether I include the section of code or not I get the same error message, despite the Microsoft.com page stating that the above code stops the error message I'm getting from appearing.

I've tried putting the context and model into my main project and that generates migrations fine, so the code for those is fine at least.

How do i add-migrations into a separate data project??

1
2
1/2/2018 11:50:51 PM

Popular Answer

Add-Migration has a -Project parameter.

Add-Migration -Project MyProject -Context MyDbContext -Name InitialMigration

EDIT: To see all options for a cmdlet (since Package Manager Console is just PowerShell) do

Get-Help Add-Migration -Full
0
10/24/2019 4:24:20 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow