borisdj / EFCore.BulkExtensions

Entity Framework EF Core efcore Bulk Batch Extensions with BulkCopy in .Net for Insert Update Delete Read (CRUD), Truncate and SaveChanges operations on SQL Server, PostgreSQL, MySQL, SQLite
https://codis.tech/efcorebulk
Other
3.68k stars 589 forks source link

EFCore 6 support (Method not found exception) #485

Closed Webreaper closed 3 years ago

Webreaper commented 3 years ago

Get the following exception with EFCore 6 preview 2. BulkExtensions works fine with EFCore 6 preview 1 (as far as I can see so far).

I know you haven't tested/updated for EFCore 5 yet, let alone 6, but just thought I'd ping this FYI. No urgency.

System.MissingMethodException: Method not found: 'Microsoft.EntityFrameworkCore.Metadata.IEntityType Microsoft.EntityFrameworkCore.ModelExtensions.FindEntityType(Microsoft.EntityFrameworkCore.Metadata.IModel, System.Type)'.
   at EFCore.BulkExtensions.TableInfo.LoadData[T](DbContext context, Type type, IList`1 entities, Boolean loadOnlyPKColumn)
   at EFCore.BulkExtensions.TableInfo.CreateInstance[T](DbContext context, IList`1 entities, OperationType operationType, BulkConfig bulkConfig)
   at EFCore.BulkExtensions.TableInfo.CreateInstance[T](DbContext context, Type type, IList`1 entities, OperationType operationType, BulkConfig bulkConfig)
   at EFCore.BulkExtensions.DbContextBulkTransaction.Execute[T](DbContext context, IList`1 entities, OperationType operationType, BulkConfig bulkConfig, Action`1 progress)
   at EFCore.BulkExtensions.DbContextBulkExtensions.BulkInsert[T](DbContext context, IList`1 entities, BulkConfig bulkConfig, Action`1 progress)
borisdj commented 3 years ago

Actually I have made test with EF 5 and nugets update, just haven't published the new version yet. Want to clean up some issues before changing Target to .NetStandard 2.1. It will be updated soon. As for the EF6, don't plan to make those changes before it is fully released. Still thx for the info. I'll reference it on https://github.com/borisdj/EFCore.BulkExtensions/issues/425 issue.

jamesgurung commented 3 years ago

Same issue here. Please consider fixing this before the EF Core 6 release, if possible. BulkExtensions is really useful, and we're trying to keep up with the EF Core 6 previews for some of the improved features/performance.

borisdj commented 3 years ago

I'll take a look after finishing migration to .Net 5.

borisdj commented 3 years ago

Lib is upgraded to v5.+ with support for .Net 5 for EF Core 5. Target in v5 is .NetStandard 2.1.

And I've tested it with EF Core 6 prev.2 in Test project where it fails with this error. However I haven't found a solution to that.

Also I even tried updating nugets, in the library project itself, to this prerelease version just for test but wasn't able to install nugets, returns error:

Package Microsoft.EntityFrameworkCore 6.0.0-preview.1.21102.2 is not compatible with netstandard2.1, supports: net5.0

ErikEJ commented 3 years ago

@borisdj you need the latest VS preview!

jamesgurung commented 3 years ago

It looks like at the time of EF Core 6 Preview 2, the ModelExtensions class was mostly empty. That's why we're getting "Method not found".

In the latest code, the class has some methods back. FindEntityType is there, but it has a different signature (returning IReadOnlyEntityType?) so we'll likely still run into issues even when the next preview comes out.

The method is marked obsolete, and the recommendation is to use IReadOnlyModel.FindEntityType(Type type). This did not exist at all in EF Core 5, so there's no obvious fix that will be compatible with both current and preview versions.

EF Core 6 now targets .NET 5 rather than .NET Standard, which is explained in this blog post. Perhaps the best option would be a multitargeting approach?

Webreaper commented 3 years ago

Any news on EFCore 6 support yet? It's only a couple of months until it'll be fully released. :)

borisdj commented 3 years ago

Doubt I'll have the time to work on this while in preview. Probably will implement it in November after full release.

Tealons commented 3 years ago

Same here, we are making ready for .NET 6 upgrade and this package is blocking. Appreciate the effort and we will wait patiently.

derigel23 commented 3 years ago

@borisdj EF Core 60 is already RC1. I'm pretty sure - no API changes will be in the release. So if you start migrate it now - with it's official release you'll need to update only library version in dependencies.

borisdj commented 3 years ago

I'll take a look at it next week with RC2.

borisdj commented 3 years ago

Have managed to make v6 NuGet 6.0.0-rc.1 released.

Webreaper commented 3 years ago

Very quick test - appears to work. Thanks for this!!

borisdj commented 3 years ago

.Net 6 fully released.

wdnijdam commented 3 years ago

It is broken on net 6.0.0

Still having ----> System.MissingMethodException : Method not found: 'Microsoft.EntityFrameworkCore.Metadata.IEntityType Microsoft.EntityFrameworkCore.ModelExtensions.FindEntityType(Microsoft.EntityFrameworkCore.Metadata.IModel, System.Type)'.

Using ALL EF packages set to 6.0.0 while the library using it was built on 5.0.13.

Webreaper commented 3 years ago

I'm using dotnet 6, EFCore 6, and bulk extensions, and it all works just fine.

When you say "the library" was built on 5, which library?

wdnijdam commented 2 years ago

I created a NuGet package based on net 5.0.13 using EF core 5.0.12. I used the package from an net6.0 project. In the net6 project I forced the EF core version to 6.0.0. Then I receive the Method not found exception.

I solved it now by adding a net6.0 target for my package based on ef core 6.0.0. When using that from the net6 project the error is not occuring.

I checked al namespaces and type names. There are no differences that can explain the Method not found. One possible cause may be a difference in nullability. The linker may see different types due to differences in nullability.

tobysainsbury commented 1 year ago

It is broken on net 6.0.0

Still having ----> System.MissingMethodException : Method not found: 'Microsoft.EntityFrameworkCore.Metadata.IEntityType Microsoft.EntityFrameworkCore.ModelExtensions.FindEntityType(Microsoft.EntityFrameworkCore.Metadata.IModel, System.Type)'.

Using ALL EF packages set to 6.0.0 while the library using it was built on 5.0.13.

Not working for me in .NET 6.0 either - I get the same error. All EF packages are version 6.0.24.

Edit: Managed to resolve this. Turns out the issue was a nuget package that I was using - despite the package dependencies in Visual Studio telling me the version of EF this particular package was using was 6.0.24, it was actually using EF 2, and an extension method only available in EF 2 (IModel.FindEntityType()).

This package was one of my own, so I was able to open the code and investigate - would never have figured this out if I didn't have access to the source code. I upgraded the framework of the package from netcore2.2 to net6.0, upgraded EF from 2 to 6 and refactored the troublesome code by casting the instance of IModel to IReadOnlyModel, as is the equivalent extension method for .NET 6.0/7.0 (see here: https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.metadata.ireadonlymodel.findentitytype?view=efcore-7.0):

var readonlyModel = (IReadOnlyModel)_dbContext.Model; var entityTypeConfig = readonlyModel.FindEntityType(typeof(TEntity));

Installed the new version of the package into my solution and the error disappeared.