簡體   English   中英

Entity Framework Core 2.0 Add-Migration 不創建任何遷移文件

[英]Entity Framework Core 2.0 Add-Migration doesn't create any migration files

最近從 EF Core 1.0 遷移到 EF Core 2.0,並且運行良好。 今天我添加了一個新表(代碼優先)並將其添加到我的 DbContext 中:

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

當我運行遷移時,我的 DbContext 位於一個單獨的項目中(請記住,這之前都是有效的),遷移結束,但沒有創建遷移文件。 這一切都在 Core 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.

沒有對我來說似乎很明顯的錯誤,也沒有創建遷移文件。 我已經完成了刪除遷移以開始清理,但仍然沒有工作。

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

<PropertyGroup>
  <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> 
</PropertyGroup>

我必須將一個項目標記為啟動項目。

您需要將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();
}

從遷移項目中刪除所有IDesignTimeDbContextFactory<TContext>實現,並在啟動類的ConfigureServices方法中注冊DbContext ,與運行應用程序時注冊的方式相同。

然后像往常一樣運行您的遷移命令,經過數小時的頭痛,這對我有用。

我有一個類似的問題,但提供的答案都沒有解決它。 在查看我的.csproj-file ,我發現 Visual Studio 在某個時候自動添加了以下幾行:

<ItemGroup>
  <Compile Remove="Migrations\**" />
  <Content Remove="Migrations\**" />
  <EmbeddedResource Remove="Migrations\**" />
  <None Remove="Migrations\**" />
</ItemGroup>

刪除此ItemGroup ,更新我的數據庫再次正常工作。

只需嘗試查看遷移 ModelSnapshot.cs 文件,以了解表名稱、關系創建(在代碼末尾)的差異。

DbContext 類應該具有帶選項的無參數構造函數和構造函數。 您必須覆蓋 OnConfiguring。 例如在它內部指定了提供者:

options.UseSqlServer(_connectionString);

代碼示例:

public class AppDbContext : DbContext
{
    private const string _connectionString = "Data Source=....;App=EntityFramework";


    public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options)
    { }

    public AppDbContext() : base()
    {
        
    }


    public virtual DbSet<MyEntitie> Users { get; set; }


    protected override void OnConfiguring(DbContextOptionsBuilder options)
    {
        if (options.IsConfigured)
        {
            return;
        }

        options.UseSqlServer(_connectionString);
    }
}

最后使用命令行並運行此命令(鍵入項目的文件名,您的 DbContext 文件所在的位置),或者您可以使用 nuget 包管理器控制台:

dotnet ef migrations -v -p App.csproj add Init

我嘗試更新 .csproj 文件中的運行時版本以包括:

<RuntimeFrameworkVersion>2.0.3</RuntimeFrameworkVersion>

在 PropertyGroup 標簽中。 它對我有用。

請參閱: Russell Seamer 的解決方案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM