1

I would like to map an entity with a complex type to another entity. However, on the last line in the Main method a

System.NotSupportedException: Cannot compare elements of type 'ConsoleApplication2.ComplexType'. Only primitive types, enumeration types and entity types are supported.

exception is raised.

How to fix it?

I am aware I can flatten the complex type properties to the entity. However, it involves lots of code duplication so I strongly prefer a solution which does not need me to flatten the complex type properties to the entity. The complex type my itself contain other complex type, so the solution should allow nesting complex types to complex types.

Entity Framework 6.1.3, AutoMapper 3.3.1 and 4.0.4, SQL Server 2014 SP1 Express, VS 2015, .NET 4.5.2 and 4.6.

It works if .SingleOrDefault(x => x.Id == 1) or .ProjectTo<MappedEntity>() is removed.

If still does not work if the .SingleOrDefault(x => x.Id == 1) is rewritten as .Where(x => x.Id == 1).ToList().SingleOrDefault();.

GitHub Issue: https://github.com/AutoMapper/AutoMapper/issues/925

using System.Data.Entity;
using System.Linq;
using AutoMapper;
using AutoMapper.QueryableExtensions;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Database.SetInitializer(new DropCreateDatabaseAlways<ClientContext>());

            Mapper.CreateMap<Entity, MappedEntity>();
            Mapper.CreateMap<ComplexType, MappedComplexType>();

            var clientContext = new ClientContext();
            clientContext.Set<Entity>().ProjectTo<MappedEntity>().SingleOrDefault(x => x.Id == 1);
        }
    }

    class ClientContext : DbContext
    {
        public virtual DbSet<Entity> Entities { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Entity>().HasKey(x => x.Id);
            modelBuilder.ComplexType<ComplexType>();
        }
    }

    class Entity
    {
        public virtual int Id { get; set; }

        public virtual ComplexType ComplexType { get; set; }
    }

    class ComplexType
    {
        public virtual string Field { get; set; }
    }

    class MappedEntity
    {
        public virtual int Id { get; set; }

        public virtual MappedComplexType ComplexType { get; set; }
    }

    class MappedComplexType
    {
        public virtual string Field { get; set; }
    }
}
8
  • I might be being a little dumb here, but where are you actually calling the mapping? You are creating the map, but I cannot see anywhere where you actually try and use it! Commented Oct 7, 2015 at 15:12
  • .ProjectTo - on the line which is breaking. Used to map lists/data sets rather than a single object. The problem could be that you are trying to map a set which hasn't been pulled into memory. Try doing clientContext.Set<Entity>().ToList().ProjectTo<MappedEntity>() Commented Oct 7, 2015 at 15:14
  • If you remove the .ProjectTo, does it work? Commented Oct 7, 2015 at 15:15
  • @Vlad274 Yes, without the projecting, no exception is raised. Commented Oct 7, 2015 at 15:16
  • 1
    @alik I haven't come across this problem but thought it might have been down to linq to entities not liking ProjectTo, I wasn't aware that it was compatible. My bad. To figure it out it might be better to break up that line into several, var set = clientContext.Set<Entity>(); var pojected = set.ProjectTo.. and so on, then we will know which part is causing the issue. Commented Oct 7, 2015 at 15:21

1 Answer 1

1

This can help:

....Configuration.AllowNullDestinationValues = false;

But this cause problems on other maps! So we can set it for our ComplexType from config:

....cfg.ForAllPropertyMaps(p => p.SourceType == typeof(ComplexType), (p, q) => { q.AllowNull(); });
  • related useful links:

https://www.csharpcodi.com/csharp-examples/AutoMapper.IProfileExpression.ForAllPropertyMaps(System.Func,%20System.Action)/

https://docs.automapper.org/en/stable/10.0-Upgrade-Guide.html?highlight=AllowNullDestinationValues#allownull-allows-you-to-override-per-member-allownulldestinationvalues-and-allownullcollections

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.