NotSuportedException when mapping entities with complex types using AutoMapper and Entity Framework
See original GitHub issueI 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:
- Created 8 years ago
- Reactions:1
- Comments:14 (8 by maintainers)
Top GitHub Comments
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:
The .AllowNull() will skip the null checking.
HTH, Ryan
In your DTO, one way to fix it is to flatten your types. Don’t do a child DTO, do this:
Your DTOs should be as flattened as possible, and only contain child DTO classes for things like child collections.