Is automapper preventing lazy loading with EF?
Is automapper preventing lazy loading with EF?
问题
I have been using an AutoMapper and it seems that it gets me all the child entities (even if I don't specify them in "Include()" clause).
Is there any way how to make lazy loading possible and get child properties only if I specify them.
Thank you,
Jakub
回答1
After mapping you will have mapped object without any references to source entity (which holds database context for lazy loading). Only property values are copied to destination entity. So you will not be able to do any lazy-loading without source entity.
Actually lazy loading works just fine for you - and it's occur during mapping process. You specified mappings for lazy-loaded properties of your entity, and mapper tries to get those values. That results in lazy-loading all navigation properties which you have configured for mapping. This is very inefficient. To disable lazy-loading during mapping you can ignore navigation properties in mapping configuration. E.g. if you have customer with lazy-loaded orders:
Mapper.CreateMap<Customer, CustomerDto>()
.ForMember(s => s.Orders, m => m.Ignore());
Or remove Orders
property from your destination entity CustomerDto
. If you need to have CustomerDto
instance with orders inside, then best option is to do eager loading of orders, to avoid additional queries.
回答2
I think the best way is to define your mapping objects according to your need. In the mapping object define only the needed child entities.Let's say some DTOs like this mapping in to the Person
entity in the domain.
class PersonDto
{
public string Name{get;set;}
public PersonLiteDto Parent{get; set;}
}
class PersonLiteDto
{
public string Name{get;set;}
//no navigation properties here..
}
Lazy Loading after mapping
I use Entity Framework and I want to map navigation properties to my DTO's.
So, my question is EF's navigation properties by default have Lazy loading, its possible with auto mapper
after mapping to have the same functionality of lazy loading to my DTO's.
But if you could do smth like that, those wouldn't be DTO-s anymore, because they would need the DB connection. What are you trying to do?
My intention is to separate completely the ORM Layer from my model. One reason to do this is because maybe in the future the Project Leader will decide to change the ORM. So, to achieve this, I don't want my repositories to return or take Persistent Model directly but Domain Model which will mapped to PM and after submitted for the specific job to the ORM?
Having DTO-s lazy load stuff goes against the decoupling you're trying to achieve. Adding another layer over your ORM complicates things. And it only makes sense if this extra layer adds real value. And if it's able to do that, certainly the shape of those objects would not be close to the shape of your DB, so AutoMapper would be of little use. I think you're on the wrong track here.
I've changed ORMs several times on various projects, and I promise you,
layering and indirection makes it MUCH harder to do so, even impossible.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-08-25 Infralution.Localization.Wpf
2017-08-25 Logical Operators (Transact-SQL)
2016-08-25 并行parallel和并发concurrent的区别
2015-08-25 IIS中的Application.CommonAppDataPath
2014-08-25 C#中的编译开关
2014-08-25 VS中自动选择x86或x64的dll