isukces / EfCore.Shaman

Fix some EntityFrameworkCore flaws.
MIT License
22 stars 8 forks source link

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

Open borisdj opened 7 years ago

borisdj commented 7 years ago

.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 commented 7 years ago

@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. @

ghost commented 7 years ago

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

isukces commented 7 years ago

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.

borisdj commented 7 years ago

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.

isukces commented 7 years ago

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 commented 7 years ago

Just finished

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

borisdj commented 7 years ago

Latest versions work with EFCore2.0. Great work.

Systemdir commented 7 years ago

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>
isukces commented 7 years ago

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.

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

Systemdir commented 7 years ago

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

Jose7777 commented 7 years ago

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

Thank you for your hard work.

Systemdir commented 7 years ago

Still no information on nuget release?

isukces commented 7 years ago

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

Systemdir commented 7 years ago
<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.

borisdj commented 7 years ago

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 commented 7 years ago

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