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