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

  • SEARCH
  • Home
  • 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 8829759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:54:30+00:00 2026-06-14T07:54:30+00:00

Note: I have also asked this question on the AutoMapper mailing list My MVC

  • 0

Note: I have also asked this question on the AutoMapper mailing list

My MVC application essentially has two levels of mapping (simplified for this question):

RepositoryObject <-> Entity <-> ViewModel

We have created two profiles, each handling the configuration for the appropriate mapping level.

The “RepositoryObjects” are eventually serialised to XML and used in REST web services. The problem we found was that an empty collection in the RepositoryObject would serialise down to an empty element in the XML, and this would cause an issue as the web service is either expecting no element, or an element containing data.

We were able to resolve this using the AllowNullCollections configuration setting. This will (of course) create a null collection rather than an empty collection, which then serialises fine.

However I’m not entirely comfortable having this as a global setting as because as Jimmy has pointed out, its not really best practice. I’m happy to have it in the RepositoryObject <-> Entity mapping because the RepositoryObjects are auto-generated (so are ugly anyway) and its very low-level in the application. But I’d prefer to not “corrupt” the Entity <-> ViewModel mapping if possible.

So, is it possible to configure this setting per profile?

Thanks.

Update

I have created test code here: https://gist.github.com/4069909

Copied here for reference:

ProfileClasses.cs

namespace NullCollectionIssue
{
    using System.Collections.Generic;

    public class SourceProfileOne
    {
        public ICollection<string> Stuff { get; set; }
    }

    public class DestProfileOne
    {
        public ICollection<string> Stuff { get; set; }
    }

    public class SourceProfileTwo
    {
        public ICollection<string> Stuff { get; set; }
    }

    public class DestProfileTwo
    {
        public ICollection<string> Stuff { get; set; }
    }
}

AutoMapperConfigurator.cs

namespace NullCollectionIssue
{
    using AutoMapper;

    public class ProfileOne : Profile
    {
        public override string ProfileName
        {
            get
            {
                return "ProfileOne";
            }
        }

        protected override void Configure()
        {
            AllowNullCollections = true;
            Mapper.CreateMap<SourceProfileOne, DestProfileOne>();
        }
    }

    public class ProfileTwo : Profile
    {
        public override string ProfileName
        {
            get
            {
                return "ProfileTwo";
            }
        }

        protected override void Configure()
        {
            Mapper.CreateMap<SourceProfileTwo, DestProfileTwo>();
        }
    }

    public static class AutoMapperConfigurator
    {
        public static void Configure()
        {
            Mapper.Initialize(x =>
            {
                x.AddProfile<ProfileOne>();
                x.AddProfile<ProfileTwo>();
            });
        }
    }
}

MappingTests.cs

namespace NullCollectionIssue
{
    using AutoMapper;

    using NUnit.Framework;

    [TestFixture]
    public class MappingTests
    {
        [Test]
        public void AutoMapper_Configuration_IsValid()
        {
            AutoMapperConfigurator.Configure();
            Mapper.AssertConfigurationIsValid();
        }

        [Test]
        public void AutoMapper_ProfileOne_AllowsNullCollections()
        {
            AutoMapperConfigurator.Configure();
            Mapper.AssertConfigurationIsValid();

            var source = new SourceProfileOne { Stuff = null };
            var dest = Mapper.Map<SourceProfileOne, DestProfileOne>(source);

            Assert.That(dest, Is.Not.Null);
            Assert.That(dest.Stuff, Is.Null);
        }

        [Test]
        public void AutoMapper_ProfileTwo_DoesntAllowNullCollections()
        {
            AutoMapperConfigurator.Configure();
            Mapper.AssertConfigurationIsValid();

            var source = new SourceProfileTwo { Stuff = null };
            var dest = Mapper.Map<SourceProfileTwo, DestProfileTwo>(source);

            Assert.That(dest, Is.Not.Null);
            Assert.That(dest.Stuff, Is.Not.Null);
            Assert.That(dest.Stuff, Is.Empty);
        }
    }
}

The tests AutoMapper_Configuration_IsValid and AutoMapper_ProfileTwo_DoesntAllowNullCollections pass, but the test AutoMapper_ProfileOne_AllowsNullCollections fails because dest.Stuff is not null.

  • 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-06-14T07:54:31+00:00Added an answer on June 14, 2026 at 7:54 am

    You are getting this behaviour because you are calling the static Mapper.Map in your configure method:

    protected override void Configure()
    {
        Mapper.CreateMap<SourceProfileTwo, DestProfileTwo>();
    }
    

    should instead be

    protected override void Configure()
    {
        CreateMap<SourceProfileTwo, DestProfileTwo>();//leading Mapper. removed
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NOTE:: I have also asked this question on the Clearcanvas forums at:: http://www.clearcanvas.ca/dnn/tabid/69/afv/topic/aff/11/aft/15086/Default.aspx Hi,
I know this question has been asked multiple number of times and i have
I have a note and it has a certain number of attributes. It also
I have seen this question asked in a couple of different ways on SO
I know this sort of question has been asked before , but I still
Variants of this question have been asked several times now here, but my question
I know this question would have been asked several time but still I am
Similar to this question asked on Stackoverflow, using JAXB to represent a list as
I know this has been asked before, but the only answers I have found
Yes, I know that general forms of this question have been asked time and

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.