2

最近從EF核心1.0遷移到EF核心2.0,它運行良好。今天,我添加了一個新表(代碼優先),並將其與添加到我的DbContext:在一個單獨的項目實體框架核心2.0添加遷移不會創建任何遷移文件

public virtual DbSet<Foo> Foos { get; set; } 

當我運行遷移,與我的DbContext(記住,這是所有工作之前),遷移結束,但沒有創建遷移文件。這是所有工作之前,核2.0使用:

http://paultechguy.blogspot.com/2017/04/entity-framework-core-migrating-and.html

PM> Add-Migration GradePremade -project Mfc.MfcRepositoryModel -verbose -context MfcDbContext -StartupProject web.xyz 
Using project 'class.xyz\Mfc.MfcRepositoryModel'. 
Using startup project 'web.xyz'. 
Build started... 
Build succeeded. 
C:\Program Files\dotnet\dotnet.exe exec --depsfile C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\web.xyz.deps.json --additionalprobingpath C:\Users\pcarver\.nuget\packages --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\web.xyz.runtimeconfig.json "C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.entityframeworkcore.tools\2.0.0\tools\netcoreapp2.0\ef.dll" migrations add GradePremade --json --context MfcDbContext --verbose --no-color --prefix-output --assembly C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\Mfc.MfcRepositoryModel.dll --startup-assembly C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\web.xyz.dll --project-dir C:\development\xyz\class.xyz\Mfc.MfcRepositoryModel --root-namespace Mfc.MfcRepositoryModel 
Using assembly 'Mfc.MfcRepositoryModel'. 
Using startup assembly 'web.xyz'. 
Using application base 'C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0'. 
Using working directory 'C:\development\xyz\web.xyz'. 
Using root namespace 'Mfc.MfcRepositoryModel'. 
Using project directory 'C:\development\xyz\class.xyz\Mfc.MfcRepositoryModel'. 
Finding DbContext classes... 
Finding IDesignTimeDbContextFactory implementations... 
Finding application service provider... 
Finding BuildWebHost method... 
No BuildWebHost method was found on type 'xyz.Program'. 
No application service provider was found. 
Finding DbContext classes in the project... 
Found DbContext 'ApplicationDbContext'. 
Found DbContext 'MfcDbContext'. 
Using DbContext factory 'MfcContextFactory'. 
Using context 'MfcDbContext'. 
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'... 
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'. 
Finding IDesignTimeServices implementations in assembly 'web.xyz'... 
No design-time services were found. 

沒有錯誤,似乎很明顯對我來說,並不會發生遷移的文件。我做了一個移除遷移開始乾淨,但仍然沒有工作。

+0

對於咧嘴一笑,我重新命名數據庫,並重新運行加載遷移。完全相同的結果。 – user2737646

+0

您是否找到了解決方案?我得到了完全相同的問題。 – Peter

回答

1

我不得不標記項目作爲啓動項目。

0

您將需要更新您的Program類,看起來像這樣

public class Program 
{ 
    public static void Main(string[] args) 
    { 
     var host = BuildWebHost(args); 

     host.Run(); 
    } 

    // Tools will use this to get application services 
    public static IWebHost BuildWebHost(string[] args) => 
     new WebHostBuilder() 
      .UseKestrel() 
      .UseContentRoot(Directory.GetCurrentDirectory()) 
      .UseIISIntegration() 
      .UseStartup<Startup>() 
      .Build(); 
} 
+0

我的web應用程序的Program.cs已經有了。我在我的單獨的類庫庫中也有一個Program.cs。注意我在我的原始文章中提到了一個鏈接,其中包含一個單獨的類庫,其中包含一個工作存儲庫;這在EF Core 2.0和VS 2017 15.x之前運行良好。 – user2737646

+0

@ user2737646是否使用.NetCore 1.x或.NetCore 2.0? – Abdi

+0

我正在使用.NetCore 2.0,VS 2017 15.3,EF Core 2.0 – user2737646

0

從您的遷移項目的ConfigureServices方法在啓動類中刪除您的所有IDesignTimeDbContextFactory<TContext>實現並註冊DbContext,你會以同樣的方式在運行應用程序時註冊。

然後像往常一樣運行你的migrations命令,這讓我頭疼幾個小時後。在標籤的PropertyGroup

<RuntimeFrameworkVersion>2.0.3</RuntimeFrameworkVersion> 

2

.NET Core類庫中存在一個問題。成功的解決方法是將在Model項目的csproj文件中的以下內容:

<PropertyGroup> 
    <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> 
</PropertyGroup> 
+1

簡單而簡潔 - 爲我工作! – Mike

+1

我不能將其標記爲已接受,這不是我的問題,我只是愛你的答案! – Mike