dotnet / dotnet/efcore

Different LazyLoad/Proxies behavior with AutoMapper between EF6 and EF Core

Open
#32,708 4 comments 0 reactions 0 assignees View on GitHub
area-perf area-proxies customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

Hello everyone.

Recently, me and my team decided to migrate our whole web system from .NET Framework to .NET 8, and upgrade/migrate all of our frameworks too, this lead us to migrate from EF6 to EF Core.

We use AutoMapper to convert Entities (which usually comes directly from Repository, with navigations "lazy loaded") to Application Models/ViewModels. And yes, we know that is not a good practice and we are replacing ViewModels to DTOs as we can, but it is definetelly all over so we still rely on this mapping, even though we face a **little** performance issue. We are using AutoMapper 9.0.0 in both .NET Framework and .NET 8, with the **same** configuration file (except from the line ```x.ShouldMapMethod = (m => false);``` in .NET 8 version, without it, the code just don't run: https://github.com/AutoMapper/AutoMapper/issues/3988.

We have both EF Core and EF6 configurated similar when it comes to LazyLoading and Proxies, as I show bellow:
![image](https://github.com/dotnet/efcore/assets/52537196/dd9fffaa-08cb-41af-befc-9bd751c304b2)

And the AutoMapper is configured, in both:
![automapper_efcore_ev4](https://github.com/dotnet/efcore/assets/52537196/bfc4505e-411e-4ff1-9a9f-23b13d070cd7)

With that being said, there is any reason why this same code takes 13.000 miliseconds to run in .NET Framework + EF6 and **800.000** miliseconds in .NET8 + EF Core 8? I may have missed something, but there is any difference of behavior when it comes to LazyLoading/Proxy in those versions? If it loads the data from the database on demand, it is completely comprehensible to be slow, but why this expressive difference? And just for the record, the entity being converted have **lots** of navigations and those are really large in terms of other navigations and fields itself.
![image](https://github.com/dotnet/efcore/assets/52537196/792655ad-ccb9-41e0-9d62-827d75bd39b5)

I even suspected from the .NET Framework package that we used to manage scopes, Medihme.Entity, I thought it could be improving the performance somehow, but just don't make sense and i couldn't find nothing relevant about.

Updating the AutoMapper to any version post 9 do not solved the problem though.

Anyway, I've implemented a workaround to "unproxy" the entities and some sub navigations, it is not perfect because I could not load dinamically sub collections navigations, but will fit for our case and, anyway, it is temporary.

Thank you!

AutoMapper: 9.0.0
AutoMapper.Extensions.ExtensionsMapping: 3.1.2

Medihme.Entity: 1.0.0

EF Core: 8.0.0 (running in .NET 8)
EF: 6.4.4 (running in .NET Framework 4.6.1)

Database provider: SQL Server
Operating system: Windows 10
IDE for .NET 8: VS 2022 17.8.3
IDE for .NET Framework 4.6.1: VS 2019 16.11.32

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.