Is EF Core Add Migration Supported from .NET Standard Library?

后端 未结 5 2188
抹茶落季
抹茶落季 2020-12-24 12:37

We have been trying to run EF Core Migration in .Net Standard 1.6 class library, but it has been failing. But same passes very well in .Net Core 1.1 class library.

5条回答
  •  时光说笑
    2020-12-24 13:15

    All that right but there is a complexity and i would like to give an explanation.

    Case: Asp.Net Core, I have a standard library that containing Db-Context. I want to migrate this context but standard library doesn't accepting migration directly, needs a startup project.

    Solution: startup project is able to create migration indirectly

    Enable-Migrations MyMigration -Project DB-ContextProjectNameThatIsStandartLib -StartupProject CallerExecutableProjectName_LikeMVCWebProjectOrConsole
    
    Add-Migration MyMigration -Project DB-ContextProjectNameThatIsStandartLib -StartupProject CallerExecutableProjectName_LikeMVCWebProjectOrConsole
    

    We will choose Caller Project from the Package Manager Console DropDown but it will create the migration file in the library project. After that don't choose other project from the drop down and run update from the caller project directly without any argument.

    Update-Database
    

    try it strange but true

    Update: I have upgraded my project Asp.net EntityFrameworkCore 3.1.3 to 3.1.4 and those on up hasnt worked firectly and gave an error :

    The EntityFramework package is not installed on project

    and this error

    Unable to create an object of type 'DomainDbContext'. For the different patterns supported at design time

    This is an EntityFramework.Core application! I have installed also EntityFramework to the standart library. But it couldnt understand and restarted V.S. This time it needed to install entityframework to starter MVC (Web) project. I can't do that. I decide to add a new Console Application to solving this requirement. And created below application for this

    install-Package Microsoft.Extensions.Configuration
    Install-Package Microsoft.Extensions.Configuration.Json
    Install-Package Microsoft.Extensions.Configuration.CommandLine
    Install-Package Microsoft.Extensions.Configuration.EnvironmentVariables
    
    class Program
    {
        public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory
        {
            public DomainDbContext CreateDbContext(string[] args)
            {
                IConfigurationRoot configuration = new ConfigurationBuilder()
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .AddEnvironmentVariables()
                    //.SetBasePath(Directory.GetCurrentDirectory())
                    //.AddJsonFile("appsettings.json")
                    .Build();
                var builder = new DbContextOptionsBuilder();
                var connectionString = configuration.GetConnectionString("DefaultConnection");
                builder.UseSqlServer(connectionString);
                return new DomainDbContext(builder.Options);
            }
        }
    
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
    

    This time it has worked correctly! with the above commands again :) cheer

提交回复
热议问题