使用不同的程序集添加迁移

使用不同的程序集添加迁移

问题描述:

我正在做一个ASP.NET CORE 1.0.0中的项目,我正在使用EntityFrameworkCore。我有单独的组件和我的项目结构是这样的:使用不同的程序集添加迁移

ProjectSolution 
    -src 
     -1 Domain 
     -Project.Data 
     -2 Api 
     -Project.Api 

在我Project.ApiStartup

public void ConfigureServices(IServiceCollection services) 
    {    
     services.AddDbContext<ProjectDbContext>(); 

     services.AddIdentity<IdentityUser, IdentityRole>() 
       .AddEntityFrameworkStores<ProjectDbContext>() 
       .AddDefaultTokenProviders(); 
    } 

DbContext是我Project.Data项目

public class ProjectDbContext : IdentityDbContext<IdentityUser> 
{ 
    public ProjectDbContext(DbContextOptions<ProjectDbContext> options) : base(options) 
    { 

    } 

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
    { 

     var builder = new ConfigurationBuilder(); 
     builder.SetBasePath(Directory.GetCurrentDirectory()); 
     builder.AddJsonFile("appsettings.json"); 
     IConfiguration Configuration = builder.Build(); 

     optionsBuilder.UseSqlServer(
      Configuration.GetConnectionString("DefaultConnection")); 
     base.OnConfiguring(optionsBuilder); 
    } 
} 

当我尝试进行初始迁移,我收到此错误:

"Your target project 'Project.Api' doesn't match your migrations assembly 'Project.Data'. Either change your target project or change your migrations assembly. Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("Project.Api")). By default, the migrations assembly is the assembly containing the DbContext. Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project."

我看到这个错误后,我试图执行此命令位于Project.Api

dotnet ef --startup-project ../Project.Api --assembly "../../1 Data/Project.Data" migrations add Initial

,我得到这个错误:

"Unexpected value '../../1 Domain/Project.Data' for option 'assembly'"

我不t know why I get this error, when I try to execute the command with the -assembly`参数。

我无法从其他程序集创建初始迁移,我搜索了有关它的信息,但没有得到任何结果。

有人有类似的问题?

+0

您是否阅读过文档? https://docs.efproject.net/zh/latest/miscellaneous/cli/dotnet.html?highlight=workaround#targeting-class-library-projects-is-not-supported – Tseng

+0

是的,但我没有得到解决错误,我尝试了不同的命令选项--startup-project和--assebly,但我没有得到任何东西 – kdar

+0

不要使用--assembly。这是一个应该从帮助信息中隐藏的内部实现细节,但是由于https://github.com/aspnet/EntityFramework/issues/5188 – natemcmaster

所有EF命令有this check

if (targetAssembly != migrationsAssembly) 
     throw MigrationsAssemblyMismatchError; 

targetAssembly =正在以目标项目。在命令行上,它是当前工作目录中的项目。在软件包管理器控制台中,无论在该窗口右上角的下拉框中选择哪个项目。

migrationsAssembly =包含迁移代码的程序集。这是可配置的。默认情况下,这将是包含DbContext的程序集,在你的情况下是Project.Data.dll。 正如错误消息所示,您有两个选项来解决此问题

1 - 更改目标程序集。

cd Project.Data/ 
dotnet ef --startup-project ../Project.Api/ migrations add Initial 

// code doesn't use .MigrationsAssembly...just rely on the default 
options.UseSqlServer(connection) 

2 - 更改迁移程序集。

cd Project.Api/ 
dotnet ef migrations add Initial 

// change the default migrations assembly 
options.UseSqlServer(connection, b => b.MigrationsAssembly("Project.Api")) 
+0

而出现,我无法从Project.Data执行命令,因为是一个类库,它不允许执行命令,我只能在Project.Api中执行命令 – kdar

+0

谢谢,它非常有用;) –

我跑了同样的问题,发现this

我们你想在一个类库运行迁移?我也是,原来这还没有得到支持,所以我们需要解决它。

编辑:我找到了解决方案上this git repo

+0

谢谢。我看到了这个解决方案,但它看起来有点强制,但到目前为止,这是我见过的唯一解决方案。我会按照链接获取新闻 – kdar

+0

非常感谢! services.AddDbContext (options => options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”),x => x.MigrationsAssembly(“ClasslibraryGoesHere”))); – NetProvoke

目前我认为EF只支持在项目上添加迁移尚未在类库。

而刚刚侧面说明给其他人谁愿意迁移添加到特定文件夹项目中:

EF CLI不支持这个呢。我试过--data-dir但它没有工作。

唯一的工作原理是使用软件包管理器控制台:

  1. 选择默认的项目
  2. 使用-OutputDir命令参数,.eg,Add-Migration InitConfigurationStore -OutputDir PersistedStores/ConfigurationStore命令将输出mgiration到文件夹“PersistedStores/ConfigurationStore”在我的项目中。

更新为2017年10月12日

public void ConfigureServices(IServiceCollection services) 
{ 
    ... 

    string dbConnectionString = services.GetConnectionString("YOUR_PROJECT_CONNECTION"); 
    string assemblyName = typeof(ProjectDbContext).Namespace; 

    services.AddDbContext<ProjectDbContext>(options => 
     options.UseSqlServer(dbConnectionString, 
      optionsBuilder => 
       optionsBuilder.MigrationsAssembly(assemblyName) 
     ) 
    ); 

    ... 
} 

我有同样的问题,直到我注意到,包管理器控制台顶部栏=>“默认项目”被认为是“项目。数据“而不是”Project.API“。

一旦您从下拉列表中定位“Project.Data”并运行迁移,您应该没问题。

+0

感谢拼写编辑格雷厄姆。 :) –