EfCore.Shaman icon indicating copy to clipboard operation
EfCore.Shaman copied to clipboard

In EFCore2.0 Method not found: 'System.Type Microsoft.EntityFrameworkCore.Metadata.IEntityType.get_ClrType()'.

Open borisdj opened this issue 7 years ago • 16 comments

.NET Core 2.0 with EFCore 2.0 was just released 2 days ago. I am now trying to switch app from 1.1 to 2.0. I have created .NetCore2.0 app WebAPI that is targeting full .NetFramework 4.6.1 After that added few add-migration and they worked, when not using EfCore.Shaman. But when I included EfCore.Shaman and added this.FixOnModelCreating(modelBuilder) now on run add-migration getting the following error:

PM> add-migration Initial System.MissingMethodException: Method not found: 'System.Type Microsoft.EntityFrameworkCore.Metadata.IEntityType.get_ClrType()'. at EfCore.Shaman.ModelScanner.EfModelWrapper.EfModelEntityWrapper.FromIEntityType(IEntityType entityType)

It is being throw from this code segment

public static EfModelEntityWrapper FromIEntityType(IEntityType entityType)
{
    return new EfModelEntityWrapper
    {
        ClrType = entityType.ClrType,
	TableName = entityType.Relational().TableName
    };
}

I have tried this fix and it seems to work: ClrType = entityType.GetType(),

borisdj avatar Aug 16 '17 15:08 borisdj

@isukces could you publish this fix because currently it is blocking the usage of library on Core 2.0 If you want I can send you a pull request, but it's only one line of code. @

borisdj avatar Aug 25 '17 07:08 borisdj

I'm affected as well - The integration of the given fix would be greatly appreciated! Status on this issue?

ghost avatar Aug 26 '17 23:08 ghost

I afraid that it is not a solution. Actually entityType.GetType() returns type of class implementing IEntityType (i.e. Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType) not type of entity class related to DbContext. Insert following snipped into protected override void OnModelCreating(ModelBuilder modelBuilder)

IEntityType et = modelBuilder.Model.GetEntityTypes().First();
Debug.WriteLine($"ClrType={et.ClrType} from {et.ClrType.Assembly.FullName}");
Debug.WriteLine($"GetType()={et.GetType()} from {et.GetType().Assembly.FullName}");

and verify output. On my test code it returns

ClrType=EfCore.Shaman.Tests.Model.MySettingsEntity from EfCore.Shaman.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
GetType()=Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType from Microsoft.EntityFrameworkCore, Version=1.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60

A proper way to solve problem is to recompile Shaman with Ef20 + Framework461.

isukces avatar Aug 28 '17 06:08 isukces

Yes, I get the same result only the EF Version is 2.0: ...from Microsoft.EntityFrameworkCore, Version=2.0.0.0, ...

So can you recompile it or if you have suggestions on some temporary fix?

And now that besides EFCore 2.0, also NetCore 2.0 along with NetStandard 2.0 is released you could consider targeting NetStandard 2.0. Maybe not immediately but in some near future.

borisdj avatar Aug 28 '17 08:08 borisdj

At this moment I'm almost finished targetting Ef20 + Framework461. I have problem with nuget restore when TargetTrameworks in csproj contains netstandard2.0 - msbuild just crash on WriteRestoreGraphTask.

isukces avatar Aug 28 '17 08:08 isukces

Just finished

  • github repository updated
  • https://www.nuget.org/packages/EfCore.Shaman/1.0.17240.66
  • https://www.nuget.org/packages/EfCore.Shaman.SqlServer/1.0.17240.59

I hope that I'll find a way to manage nuget restore problem soon.

isukces avatar Aug 28 '17 08:08 isukces

Latest versions work with EFCore2.0. Great work.

borisdj avatar Aug 28 '17 09:08 borisdj

I still get the same error message when calling FixOnModelCreating in OnModelCreating(ModelBuilder modelBuilder):

Exception thrown: 'System.MissingMethodException' in EfCore.Shaman.dll An exception of type 'System.MissingMethodException' occurred in EfCore.Shaman.dll but was not handled in user code: 'Method not found: 'System.Type Microsoft.EntityFrameworkCore.Metadata.IEntityType.get_ClrType()'.' Stack trace: at EfCore.Shaman.ModelScanner.EfModelWrapper.EfModelEntityWrapper.FromIEntityType(IEntityType entityType) at System.Linq.Enumerable.SelectEnumerableIterator2.ToList() at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at EfCore.Shaman.ModelScanner.EfModelWrapper.FromModel(IModel model) at EfCore.Shaman.ModelScanner.ModelsCachedContainer.SetRawModel(Type type, IMutableModel model, IShamanLogger logger) at EfCore.Shaman.ModelFixer.FixOnModelCreating(ModelBuilder modelBuilder, Type contextType, ShamanOptions shamanOptions)

.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="EfCore.Shaman" Version="1.0.17240.66" />
    <PackageReference Include="JetBrains.Annotations" Version="11.0.0" />
    <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" />
  </ItemGroup>
  
  (...)
</Project>

Systemdir avatar Aug 28 '17 11:08 Systemdir

As I wrote before I have problem with targetting to netstandard2.0. You can try to compile libraries from sources.

Moreover I found big changes in EfCore2.0 and its dependences during tests on net461. In fact I was unable to migrate my private project based on Serenity into 2.0 platform. So I decided to release separate set of packages (soon on nuget).

One of the problem was with one-to-one relation defined in model that produces proper migration in 1.1.2 but crashes in 2.0. I gave up with this.

  • EfCore.Shaman + EfCore.Shaman.SqlServer (net461) designed for EfCore 2.0
  • EfCore.Shaman112 + EfCore.Shaman112.SqlServer (net451+netstandard1.6) designed for EfCore 1.1.2 for compatibility reason

You can review changes. I switched to EfCore.Shaman112 in my Serenity based project.

isukces avatar Aug 29 '17 12:08 isukces

Thank you for the clarification 👍 I look forward to the nuget release.

Systemdir avatar Aug 29 '17 13:08 Systemdir

I look forward to the nuget release too. What is the estimated release date?

Thank you for your hard work.

Jose7777 avatar Sep 13 '17 17:09 Jose7777

Still no information on nuget release?

Systemdir avatar Oct 11 '17 09:10 Systemdir

Please update information: a) what exactly nuget packages version do you use b) what is current call stack of exception. c) what is target platform/framework of your project

isukces avatar Oct 11 '17 10:10 isukces

<TargetFramework>netstandard2.0</TargetFramework>

<ItemGroup>
<PackageReference Include="EfCore.Shaman" Version="1.0.17277.68" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" />
</ItemGroup>

Warning:

  Package ' EfCore.Shaman 1.0.17277.68' was restored using '.NETFramework,Version=v.4.6.1'  
  instead of  the project target framework '.NETStandard,Version=v2.0'. 
  This package may not be fully compatible with your project.

Appart from that the library seems to work as intended.

Systemdir avatar Oct 11 '17 11:10 Systemdir

I have also just tested it (1.0.17277.68) by creating new project that is targeting .NET Core 2.0 It works but has this warning that can be annoying. (Warning NU1701 Package was restored using '.NETFramework,Version=v4.6.1' ...) Could that be resolved?

borisdj avatar Oct 13 '17 13:10 borisdj

Temporary fix to hide the warning: In Solution Explorer select this library, then on it's Properties panel add following into NoWarn field: NU1701

borisdj avatar Oct 19 '17 06:10 borisdj