Is there a way to run EF Core RC2 tools from published DLL?

后端 未结 5 1819
抹茶落季
抹茶落季 2021-02-07 03:26

After publishing a .Net Core RC1 application, commands specified in the project.json had corresponding .cmd files created for them which could be executed after deployment (e.g.

5条回答
  •  野性不改
    2021-02-07 03:26

    Within my context I have this hack from here

        // Hack added so EntityFrameworkCore\Add-Migration initial works
    public class ApplicationContextDbFactory : IDesignTimeDbContextFactory
    {
        MyContext IDesignTimeDbContextFactory.CreateDbContext(string[] args)
        {
            IConfigurationRoot configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();
    
            var optionsBuilder = new DbContextOptionsBuilder();
            optionsBuilder.UseSqlServer(configuration.GetConnectionString("StreamCheckpointContextDB"));
            return new MyContext(optionsBuilder.Options);
        }
    }
    

    I made my Program.Main read like this...

        public static void Main(string[] args)
        {
            if (args.Contains("JustMigrateMe"))
            {
                IDesignTimeDbContextFactory factory = new ApplicationContextDbFactory();
                var ctx = factory.CreateDbContext(new string[0]);
                ctx.Database.Migrate();
                ctx.Dispose();
                return;
            }
    
            // Other stuff
        }
    }
    

    So to apply migrations I simply call the exe with the added argument.

提交回复
热议问题