Add migration with different assembly

后端 未结 14 1210
感情败类
感情败类 2020-12-02 14:25

I am working on a project with ASP.NET CORE 1.0.0 and I am using EntityFrameworkCore. I have separate assemblies and my project structure looks like this:

Pr         


        
14条回答
  •  抹茶落季
    2020-12-02 14:40

    I was facing similar issue, though answers seems straight forward somehow they didn't work. My Answer is similar to @Ehsan Mirsaeedi, with small change in DbContextFactory class. Instead of Adding migration assembly name in Startup class of API, I have mentioned in DbContextFactory class which is part of Data project(class library).

    public class DbContextFactory : IDesignTimeDbContextFactory
    {
       public KuchidDbContext CreateDbContext(string[] args)
       {
           var configuration = new ConfigurationBuilder()
              .SetBasePath(Directory.GetCurrentDirectory())
              .AddJsonFile("appsettings.json")
              .Build();
    
           var dbContextBuilder = new DbContextOptionsBuilder();
    
           var connectionString = configuration.GetConnectionString("connectionString");
    
           var migrationAssemblyName= configuration.GetConnectionString("migrationAssemblyName");
    
           dbContextBuilder.UseSqlServer(connectionString, o => o.MigrationAssembly(migrationAssemblyName));
    
           return new KuchidDbContext(dbContextBuilder.Options);
       }
    }
    

    You would need 'Microsoft.Extensions.Configuration' and 'Microsoft.Extensions.Configuration.Json' for SetBasePath & AddJsonFile extensions to work.

    Note: I feel this is just a work around. It should pickup the DbContextOptions from the startup class somehow it is not. I guess there is definitely some wiring issue.

提交回复
热议问题