如何解决 找不到方法:System.Collections.Generic.IList`1<Microsoft.EntityFrameworkCore.Metadata.Conventions.IModelFinalizingConvention>

mle*_*eng 12 c# macos asp.net-mvc

目前我正在尝试在 macOS 上开发 C# (ASP.NET MVC) Web 应用程序,我在 .NET 6.0.402 上运行

当我运行dotnet ef update database更新数据库时出现此错误:

未找到方法:“System.Collections.Generic.IList`1<Microsoft.EntityFrameworkCore.Metadata.Conventions.IModelFinalizingConvention> Microsoft.EntityFrameworkCore.Metadata.Conventions.ConventionSet.get_ModelFinalizingConventions()”。

我确实摆弄了我的 Migrations-> [serial]_[name].designer.cs 文件,因为它没有自动生成信息来匹配我运行时的模型dotnet ef migrations add

笑话.cs(模型)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JokeWebApp.Models
{
    public class Joke
    {
        public int Id { get; set; }
        public string? JokeQuestion { get; set; }
        public string? JokeAnswer { get; set; }

        //ctor shortcut for constructor
         public Joke()
        {

        }
    }

}
Run Code Online (Sandbox Code Playgroud)

20221109024428_initialsetup.Designer.cs(数据->迁移)

// <auto-generated />
using System;
using JokeWebApp.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;


namespace JokeWebApp.Data.Migrations
{
    [DbContext(typeof(ApplicationDbContext))]
    [Migration("20221109024428_initialsetup")]
    partial class initialsetup
    {
        protected override void BuildTargetModel(ModelBuilder modelBuilder)
        {
#pragma warning disable 612, 618


modelBuilder.HasAnnotation("ProductVersion", "6.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

modelBuilder.Entity("JokeWebApp.Models.Joke", b =>
{
b.Property<int>("Id").ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

b.Property<string>("JokeAnswer")
.HasColumnType("nvarchar(max)");

b.Property<string>("JokeQuestion")
.HasColumnType("nvarchar(max)");

b.HasKey("Id");

b.ToTable("Joke");
});

...
Run Code Online (Sandbox Code Playgroud)

项目.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UserSecretsId>aspnet-JokeWebApp-c27aee20-1e9d-4266-993b-368018ae336f</UserSecretsId>
  </PropertyGroup>

  <ItemGroup>
    <None Update="app.db" CopyToOutputDirectory="PreserveNewest" ExcludeFromSingleFile="true" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.10" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.10" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="6.0.10" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.10" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.10" />
  </ItemGroup>

</Project>

Run Code Online (Sandbox Code Playgroud)

我不确定我是否缺少所需的软件包,或者我在摆弄该软件包时弄乱了某个地方designer.cs

有人能指出我正确的方向吗?

我确保我的包参考Project.csproj是最新的。想知道是否可能存在一些差异:

 <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0" />
Run Code Online (Sandbox Code Playgroud)

其他软件包的版本为“6.0.10”,不确定我是否应该将其作为版本,因为Microsoft.EntityFrameworkCore.SqlServer.SqlServer它是我为了访问而下载的软件包 SqlServerValueGenerationStrategy.IdentityColumn.

我还在另一个线程上读到该问题可能是由于旧版本的 DLL 造成的。如何确保所有内容都是最新文件,在重建应用程序之前我需要删除哪些构建项目?

小智 11

nuget包之间存在版本不匹配的情况。将ef core包升级到7.0.0版本即可解决您的问题。


小智 0

EntityFrameworkCore 的 NuGet 版本 7.0.0 及以上版本仅在您安装了 NET 7.0 版本时才有效,因此在您的情况下,您必须再次更新到 6.0.13,这是迄今为止的最新版本。我遇到了同样的问题,所以我希望您能够通过安装 NET 7.0 或返回到旧的参考文献来最终解决它。