question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NotSuportedException when mapping entities with complex types using AutoMapper and Entity Framework

See original GitHub issue

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. Complex types may themselves contain other complex types, 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();.

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; }
    }
}

StackOverflow: http://stackoverflow.com/questions/32995791/notsuportedexception-when-mapping-entities-with-complex-types-using-automapper-a

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

6reactions
ryandanthonycommented, Aug 11, 2016

There is a fix slated to go into 5.1.0. (it’s in the 5.1.0-alpha-01199 build on https://www.myget.org/F/automapperdev/api/v2 )

It is a problem with how automapper generates the expression, it will do a null check on the complex type, but entity framework doesn’t support null checking of complex types.

The fix (using 5.1) looks like this:

CreateMap<Db.Something, Dto.Something>()
       .ForMember(m => m.TheComplexType, opt => opt.AllowNull())

The .AllowNull() will skip the null checking.

HTH, Ryan

1reaction
jbogardcommented, Jun 8, 2016

In your DTO, one way to fix it is to flatten your types. Don’t do a child DTO, do this:

public class ParentDto
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Child1Name { get; set; }
    public string Child2Name { get; set; }
}

CreateMap<Parent, ParentDto>()
    .ForMember(dest => dest.Child1Name, opt => opt.MapFrom(origin => origin.Body.Child1.Name))
    .ForMember(dest => dest.Child2Name, opt => opt.MapFrom(origin => origin.Body.Child2.Name))

Your DTOs should be as flattened as possible, and only contain child DTO classes for things like child collections.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NotSuportedException when mapping entities with ...
The complex type my itself contain other complex type, so the solution should allow nesting complex types to complex types. Entity Framework 6.1 ......
Read more >
NotSupportedException when Mapping entities with ...
I have difficulties in mapping entities with complex types using AutoMapper and Entity Framework as it is rising a NotSupportedException ...
Read more >
User-defined function mapping - EF Core
EF Core allows for using user-defined SQL functions in queries. ... function mapping works, let's define the following entities:.
Read more >
Entity Framework projections to Immutable Types ...
Projections are when data is retrieved from an IQueryable source and mapped onto either a named class or an anonymous type - eg....
Read more >
The reasons behind why I don't use AutoMapper.
I've encountered cases when AutoMapper transforms simple thing like mapping values from object to other into a really complex problem, which ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found