簡體   English   中英

使用 Entity Framework Core 添加遷移時出現錯誤

[英]I get an error when I add migration using Entity Framework Core

我建立了一個控制台項目並首先使用代碼到 map model 到數據庫。 當我運行Add-Migration InitialMigration命令時,出現錯誤:

來自程序集“Microsoft.EntityFrameworkCore.SqlServer,版本=3.1.5.0,文化=中性,PublicKeyToken=adb9793829ddae60”的“Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServerSqlTranslatingExpressionVisitorFactory”類型中的方法“創建”沒有實現。

DbContext是:

class ActorDbContext : DbContext
{
    public DbSet<Actor> Actors { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(
            @"Server=(localdb)\mssqllocaldb;Database=ActorDb;"
            + "Trusted_Connection=True;");
    }
}

實體是:

public class Actor
{
    public int Id { get; set; }
    public String Name { get; set; }
    public int Age { get; set; }
    public bool AcademyWinner { get; set; }
}

我剛剛使用VS for Mac遇到了同樣的問題。 我的問題是我安裝了以下版本的軟件包:

  • Microsoft.EntityFrameworkCore.Tools 5.0.0-preview.8.20407.4
  • Microsoft.EntityFrameworkCore.Design 5.0.0-preview.8.20407.4
  • Microsoft.EntityFrameworkCore.SqlServer 3.1.8

注意使用的不同版本。 為了糾正這個問題,我卸載了軟件包的preview版本並安裝了最新的穩定版本。

  • Microsoft.EntityFrameworkCore.Tools 3.1.8
  • Microsoft.EntityFrameworkCore.Design 3.1.8
  • Microsoft.EntityFrameworkCore.SqlServer 3.1.8

再次注意所有 3 個軟件包的版本。 一旦我安裝了每個 package 的正確版本,問題就解決了,我的Add-Migration工作。

您必須包含以下所有軟件包才能具有相同的版本:

Microsoft.EntityFrameworkCore
Microsoft.AspNetCore.Identity.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.SqlServer

我試過了,它運行良好。

我正在添加 [mohammed-abdelwahab][1] 的答案,以下軟件包需要最新更新:

Microsoft.EntityFrameworkCore
Microsoft.AspNetCore.Identity.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.SqlServer

為此,右鍵單擊項目 --> 管理 Nuget 包 --> 單擊更新,然后一一更新,如果不存在則添加如下

 PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer
 PM> Install-Package Microsoft.EntityFrameworkCore.Tools
 PM> Add-Migration InitialMigration

祝你好運:)

也許您正在嘗試混合不同的版本。 確保所有版本都對齊。

嘗試將您的 SqlServer 更改為最新版本(5.0.0),我運行到完全相同的錯誤,一旦我更新到最新版本,它運行良好。

我在做舊教程時遇到了同樣的問題。 問題是我安裝了帶有“-pre”子句的 package。

使用以下命令:

  1. 在您的項目中登記已安裝的軟件包-
    PM> Get-Package -ProjectName <YourProjectName>

Output:

Id                                  Versions                                 ProjectName                     
--                                  --------                                 -----------                     
Microsoft.EntityFrameworkCore.Tools {5.0.0-rc.2.20475.6}                     FirstEFCoreProject              
Microsoft.EntityFrameworkCore.Sq... {3.1.9}                                  FirstEFCoreProject   
  1. 卸載不穩定的package -
    PM> Uninstall-Package Microsoft.EntityFrameworkCore.Tools
  1. 重新安裝 package(不帶 -pre)-
    PM> Install-Package Microsoft.EntityFrameworkCore.Tools
  1. 然后再次添加您的遷移 -
    PM> Add-Migration InitialMigration

我也剛遇到這個問題。 事實證明,我不小心同時安裝了Microsoft.EntityFrameworkCore.SqlServerMicrosoft.EntityFrameworkCore.Sqlite

我真的只需要Sqlite,所以我刪除了對SqlServer的引用。 然后我不得不修復幾個引用 SqlServer 的地方,而不是 Sqlite,我錯過了,現在錯誤消失了。

在 NuGet 這 2 行運行后修復

添加遷移初始

接着

更新數據庫

.NET Core 6.0.8 版本的Microsoft.EntityFrameworkCore對於所有包應該是相同的

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.8">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.8" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
  </ItemGroup>

錯誤隨之而來

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0-preview.7.22376.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.8">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.8" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
  </ItemGroup>

暫無
暫無

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

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