2017-02-13 134 views
9

我在添加一個初始遷移到.NET核心類庫裏面我的實體框架數據庫上下文問題的遷移。.NET核心實體框架 - 上下文添加在類庫

當我運行:

dotnet ef migrations add migrationName -c PlaceholderContext 

我得到錯誤:

Could not invoke this command on the startup project 'Placeholder.Data'. This version of the Entity Framework Core .NET Command Line Tools does not support commands on class library projects in ASP.NET Core and .NET Core applications. See http://go.microsoft.com/fwlink/?LinkId=798221 for details and workarounds. 

於是我點擊了link,得知這是不可能的遷移添加到類庫。但是,您可以將類庫項目轉換爲「應用程序」項目,但通過這樣做我無法從業務層(類庫)中引用此「應用程序」項目。

項目結構:

Placeholder.Web(的WebAPI)=>Placeholder.Business(類庫)=>Placeholder.Data(類庫)

Project structure

Placeholder.Web => Startup.cs

public void ConfigureServices(IServiceCollection services) 
    { 
     // Add framework services. 
     services.AddApplicationInsightsTelemetry(Configuration); 

     services.AddMvc(); 

     //HERE WE REGISTER DB CONTEXT, (STATIC CLASS IN BUSINESS LAYER) 
     services.InjectBusinessContext(@"Data Source=(localdb)\ProjectsV13;Initial Catalog=Placeholder;Integrated Security=True;Connect Timeout=30;"); 

     services.InjectWebServices(); 
     services.InjectBusinessServices(); 
    } 

我怎樣才能克服這個真的很煩人的問題?

更新(1)

我已經轉換我Placeholder.Data類庫到「應用程序」有靜態主要方法。因爲我無法再引用Placeholder.Business中的Placeholder.Data,所以我必須使用microsoft doc頁面上列出的解決方法2。當我現在運行遷移腳本我得到如下:

No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext

衛生署ofcourse這不會工作,在的DbContext是從我Placeholder.Web應用程序註冊(通過業務層)。然後我唯一的選擇就是在新的靜態main方法添加新的上下文,我真的不希望這樣做..

回答

6

你並不需要「轉換」你的數據項目的應用程序。下面是一個類似的結構的測試程序:

project structure

在數據項目project.json,添加asp.net核心的NuGet包。

project.json

現在,要創建一個遷移,僅僅在數據項目點擊右鍵,選擇「在文件瀏覽器打開文件夾」,然後在文件瀏覽器,按Shift +右鍵點擊「打開命令窗口在這裏。 「

若要遷移,只需指定 '啓動項目' 爲Web應用程序(其中startup.cs存在)

dotnet ef --startup-project ../TestPatterns2.Web migrations add Second 

migrations

瞧,遷移:

second migration

將遷移文件夾添加到數據項目中: 當您定義服務ce,像這樣添加遷移點

services.AddDbContext<ApplicationDbContext>(options => 
      options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), b => b.MigrationsAssembly("TestPatterns2.Data"))); 
+0

我以後會在我回家時試試這個! – Reft

+0

如果它適合你,請標記爲答案。如果您想更進一步,您還可以在DATA類庫項目中擴展IServiceCollection,並在其中添加DI和連接字符串,以便您的Web項目完全獨立於DATA項目。 – Reza

+0

對不起,延遲迴復,只是嘗試了你的代碼,它的工作原理,歡呼聲。但!如果你看看我的項目結構,我有一個名爲migrations的自創文件夾。當我運行命令時,我在根目錄中得到一個新的遷移文件夾?我能改變這個嗎? 2.如果我想在第一次初始遷移後添加更多遷移,我是否需要再次執行這些步驟,或者可以使用包管理器控制檯以正常方式創建它? – Reft

1

它只需要爲使遷移的應用程序(需要一個入口點),以便使後圖書館的一個應用程序,並創建您的遷移,註釋掉您的project.json中的buildOptionsruntimes元素。現在它將再次建成一個圖書館。

取消其註釋,每當你需要添加另一個遷移。

+0

我試過從「netstandard1.6」轉換爲「netcoreapp1.0」,但我得到的.Data與netstandard1.6不兼容。 – Reft

+0

好吧,我不得不卸載業務層,(參考),然後它的工作。我將類庫轉換爲一個應用程序。因爲我無法從.Business中引用.Data。我不得不使用Workaround 2。似乎我需要使用main方法在.Data中註冊上下文?我不想這樣做.. – Reft

+0

@Reft我使用的解決方案是爲它聲明一個DBFactory。我不記得我發現要做的博客文章,對不起。 – BradleyDotNET