2016-08-01 102 views
2

我正在做一個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

回答

7

所有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

謝謝,它非常有用;) –

2

我跑了同樣的問題,發現this

我們你想在一個類庫運行遷移?我也是,原來這還沒有得到支持,所以我們需要解決它。

編輯:我找到了解決方案上this git repo

+0

謝謝。我看到了這個解決方案,但它看起來有點強制,但到目前爲止,這是我見過的唯一解決方案。我會按照鏈接獲取新聞 – kdar

+0

非常感謝! services.AddDbContext (options => options.UseSqlServer(Configuration.GetConnectionString(「DefaultConnection」),x => x.MigrationsAssembly(「ClasslibraryGoesHere」))); – NetProvoke

0

目前我認爲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) 
     ) 
    ); 

    ... 
} 
1

我有同樣的問題,直到我注意到,包管理器控制檯頂部欄=>「默認項目」被認爲是「項目。數據「而不是」Project.API「。

一旦您從下拉列表中定位「Project.Data」並運行遷移,您應該沒問題。

+0

感謝拼寫編輯格雷厄姆。 :) –

相關問題