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

  • 相关阅读:
    八十四、SAP中的ALV创建之三,创建ALV表格
    八十三、SAP中的ALV创建之二,ALV相关的类型池定义
    八十二、SAP中的ALV创建之一,新建一个程序
    八十一、SAP中的ALV的简介(ABAP List Viewer)
    八十、SAP中数据库操作之 (FOR ALL ENTRIES IN )用法,比较难明白
    七十九、SAP中数据库操作之更新数据,UPDATE的用法
    七十八、SAP中数据库操作之查询条数限制
    七十七、SAP中数据库操作之多表联合查询
    七十六、SAP中数据库的查询用法之 COUNT(总数),SUM(求和),AVG(求平均),GROUP BY(分组)
    七十五、SAP中数据库的使用SQL
  • 原文地址:https://www.cnblogs.com/chucklu/p/16623270.html
Copyright © 2020-2023  润新知