dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.65k stars 3.15k forks source link

Method not found: 'System.Type Microsoft.EntityFrameworkCore.Metadata.ITypeBase.get_ClrType()'. #26701

Closed xiaomazz closed 1 year ago

xiaomazz commented 2 years ago

ef6 数据库迁移时,


protected override void OnModelCreating(ModelBuilder builder)
        {

            base.OnModelCreating(builder);
}       

报错

Method not found: 'System.Type Microsoft.EntityFrameworkCore.Metadata.ITypeBase.get_ClrType()'.
ajcvickers commented 2 years ago

@xiaomazz Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

roji commented 2 years ago

Note there's a good chance you're mixing versions of EF Core; check your csproj and make sure the same version for all EF-related packages.

supapo commented 2 years ago

Hi, I got the same error after upgrading to .net 6 It happens every time we interact with the DB, including dotnet ef database update This is my csproj file

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

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <OutputType>Exe</OutputType>
    <AssemblyName>proj</AssemblyName>
    <RootNamespace>MyProject</RootNamespace>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)'=='Release'">
    <RuntimeIdentifier Condition=" '$(OS)' == 'Windows_NT' ">win-x64</RuntimeIdentifier>
    <RuntimeIdentifier Condition=" '$(OS)' == 'Unix' ">linux-x64</RuntimeIdentifier>
    <SelfContained>true</SelfContained>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="IdentityModel" Version="5.2.0" />
    <PackageReference Include="Keycloak.Net" Version="1.0.18" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.ApiDescription.Client" Version="6.0.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="NetEscapades.Configuration.Yaml" Version="2.1.0" />
    <PackageReference Include="NodaTime" Version="3.0.9" />
    <PackageReference Include="NodaTime.Serialization.JsonNet" Version="3.0.0" />
    <PackageReference Include="Npgsql" Version="6.0.0" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.0" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="6.0.0" />
    <PackageReference Include="Npgsql.NodaTime" Version="6.0.0" />
    <PackageReference Include="stateless" Version="5.11.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
    <PackageReference Include="TimeZoneConverter" Version="3.5.0" />
    <PackageReference Include="Toolbelt.EntityFrameworkCore.IndexAttribute" Version="5.0.0" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="Migrations\" />
  </ItemGroup>

</Project>

Thanks in advance

ajcvickers commented 2 years ago

@supapo I am able to build and run a simple application with that project file. Can you attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

ajcvickers commented 2 years ago

@supapo Also, the output of running dotnet ef database update --verbose would be very useful.

asiena commented 2 years ago

I'm facing a similar problem and also using npgsql packages. Is it possible this is the cause?

asiena commented 2 years ago

Project file for reference

<Project Sdk="Microsoft.NET.Sdk">
  <Import Project="..\..\common.props"></Import>
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <RootNamespace>example</RootNamespace>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
    <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="DocumentFormat.OpenXml" Version="2.14.0" />
    <PackageReference Include="ExcelDataReader" Version="3.6.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.0" />
    <PackageReference Include="Npgsql.Json.NET" Version="6.0.0" />
  </ItemGroup>
</Project>
roji commented 2 years ago

@asiena I've copy-pasted your csproj and made a minimal EF Core application, and everything works fine. Can you please prepare a runnable project which shows the exception happening?

michaelroeth commented 2 years ago

I got the same error and was able to reproduce it. Please check https://github.com/michaelroeth/EntityFrameworkError

I have a .net6 Application (with .net6 efcore references) referencing a .netstandard2.0-Entity-Framework project.

ErikEJ commented 2 years ago

@michaelroeth you library references EF Core 3.1!

michaelroeth commented 2 years ago

@ErikEJ I'm aware of this and I should have mentioned it. This is caused by the fact, that we have a legacy .net4.7 rich client application that is referencing the netstandard2.0 project. I just wanted to mention that this reproduces the exact error and it worked before with .net5

vmutlu commented 2 years ago

Hello, i am facing the same problem. The entityframework packages are all in the same version. Is there a definitive solution to this problem? Note: My application is being developed with .net 6 version.

Error Received: Method not found: 'System.Type Microsoft.EntityFrameworkCore.Metadata.IPropertyBase.get_ClrType()'.

ajcvickers commented 2 years ago

@vmutlu Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

ajcvickers commented 2 years ago

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

ghost commented 2 years ago

@ajcvickers after hitting this locally, AFAICT it's due to the ClrType property moving from the ITypeBase interface up to its 'parent' interface of IReadOnlyTypeBase as part of the changes in EF core 6.0?

5.0, present on ITypeBase https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.metadata.itypebase.clrtype?view=efcore-5.0#microsoft-entityframeworkcore-metadata-itypebase-clrtype

6.0, missing from ITypeBase https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.metadata.itypebase?view=efcore-6.0

6.0, present on IReadOnlyTypeBase https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.metadata.ireadonlytypebase.clrtype?view=efcore-6.0#microsoft-entityframeworkcore-metadata-ireadonlytypebase-clrtype

So anything that builds against pre-6.0 referencing the ClrType property (in my example, off of the objects returned from builder.Model.GetEntityTypes()) and runs against 6.0 or later is going to hit this, at least AFAICT?

I tried making a fairly minimal repro at https://github.com/jamesmanningrapidscale/clrtype-missing-repro which just has a net6 console app use a netcore 3.1 class lib with an EF context

C:\Dev\clrtype-missing-repro\clrtype-missing-repro » dotnet run Unhandled exception. System.MissingMethodException: Method not found: 'System.Type Microsoft.EntityFrameworkCore.Metadata.ITypeBase.get_ClrType()'.

Should I open a new issue or can this one be reopened? Or is this By Design and having different build and runtime EF versions like this isn't supported?

Thank you!

AndriySvyryd commented 2 years ago

@jamesmanningrapidscale This is a known breaking change. As a workaround you can invoke these methods through reflection.

ghost commented 2 years ago

@AndriySvyryd sounds good to me, thank you!