Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6895683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:56:23+00:00 2026-05-27T06:56:23+00:00

Folks, I am mapping EF objects to simple Pocos using automapper in my service

  • 0

Folks,
I am mapping EF objects to simple Pocos using automapper in my service layer. I have certain entities that have many relationships in EF,but I want to restrict how much of this gets pulled back from the db. One of these entities would map to a database table but would have many relationships to other tables that would appear as entity collections in the generated model (EDMX).

So I have created a “shallow” poco for the entity which only has poco properties for the first level of properties in the entity, i.e. some integer Ids instead of associated collections/entity collections. I map in the following manner….

var simplepocoforentity= Mapper.Map(_readOnlyDb.Single<EfEntity>(x => x.Id== Id),
                                    new SimplPocoForEntity());

My question is.. because I am only mapping the entity to a simple poco here, can I be confident that EF will not try and query other non referenced data from the underlying db tables relationships when I do the mapping?

I know that I can investigate this via SQL Profiling, but I would appreciate any input befor going down that route.
Thanks
K.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:56:23+00:00Added an answer on May 27, 2026 at 6:56 am

    It depends on the internal implementation of AutoMapper. I would assume that AutoMapper does not try to access the navigation properties of the entity (in which case lazy loading and an additional database query would kick in). I don’t see a reason why AutoMapper should do this if you only map scalar properties. But who knows…

    If you want to be on the safe side you could disable lazy loading temporarily:

    try
    {
        _readOnlyDb.ContextOptions.LazyLoadingEnabled = false;
    
        var simplepocoforentity = Mapper.Map(_readOnlyDb.Entities
            .Single(x => x.Id == Id), new SimplPocoForEntity());
    
        // ...
    }
    finally
    {
        _readOnlyDb.ContextOptions.LazyLoadingEnabled = true;
    }
    

    As a side note: Be aware that you load the full entity into the context with this approach. After that the mapping happens. Potentially you load more columns from the database than you really need (perhaps not in your special case here). In general I would do the mapping with a projection which ensures that the database only queries the columns which are needed:

    var simplepocoforentity = _readOnlyDb.Entities
        .Where(e => e.Id == Id)
        .Select(e => new SimplPocoForEntity
        {
            PocoProperty1 = e.EntityProperty1,
            PocoProperty2 = e.EntityProperty2,
            PocoProperty3 = e.EntityProperty3
            // etc.
        })
        .Single();
    

    With this approach no lazy loading would happen at all because you don’t load the entity but directly the PocoForEntity from the database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Folks, We have been using OpenXml APIs found in System.IO.Packaging for creating a package
Folks, I have just joined a company where they are using WSAD 5.1 which
Folks, I had a quick question about Amazon EC2, I have been recently using
folks! I have Service, which checks in onStartCommand() whether auto update was set in
I have an existing NHibernate 2.1.2.4000 mapping that uses the mapping syntax: <database-object> <create>
I have a really simple setup to try out a bidirectional mapping with annotations:
Folks, In a layered .Net solution that we have recently deployed, we now realise
Folks, I am working on an app that hosts certain pages from a different
Folks, I need a solution that allows drag-and-drop cell over the table. The cells
Folks, we all know that IP blacklisting doesn't work - spammers can come in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.