简体   繁体   English

使用 AutoMapper 和实体框架映射具有复杂类型的实体时出现 NotSuportedException

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

I would like to map an entity with a complex type to another entity.我想将具有复杂类型的实体映射到另一个实体。 However, on the last line in the Main method a但是,在Main方法的最后一行中

System.NotSupportedException: Cannot compare elements of type 'ConsoleApplication2.ComplexType'. System.NotSupportedException:无法比较“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.实体框架 6.1.3、AutoMapper 3.3.1 和 4.0.4、SQL Server 2014 SP1 Express、VS 2015、.NET 4.5.2 和 4.6。

It works if .SingleOrDefault(x => x.Id == 1) or .ProjectTo<MappedEntity>() is removed.如果.SingleOrDefault(x => x.Id == 1).ProjectTo<MappedEntity>()被删除,它会起作用。

If still does not work if the .SingleOrDefault(x => x.Id == 1) is rewritten as .Where(x => x.Id == 1).ToList().SingleOrDefault();如果.SingleOrDefault(x => x.Id == 1)被重写为.Where(x => x.Id == 1).ToList().SingleOrDefault();如果仍然不起作用. .

GitHub Issue: https://github.com/AutoMapper/AutoMapper/issues/925 GitHub 问题: 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; }
    }
}

This can help:这可以帮助:

....Configuration.AllowNullDestinationValues = false;

But this cause problems on other maps!但这会导致其他地图出现问题! So we can set it for our ComplexType from config:所以我们可以从配置中为我们的 ComplexType 设置它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在实体框架中返回具有复杂类型的实体的函数映射存储过程 - Function Mapping Stored Procedures That Return Entities With Complex Types In Entity Framework 自动映射器和实体框架映射复杂的关系建议 - Automapper & Entity Framework mapping complex relations suggestions 使用automapper将复杂的DTO映射到实体 - Mapping complex DTOs to entities with automapper 如何在使用空间类型和自动化时优化实体框架查询? - How to optimize Entity Framework query when using spatial types and automapper? 映射包含导航属性的实体框架实体时,Automapper异常 - Automapper Exception when mapping Entity Framework entity that contains a navigation property 使用实体框架的复杂关系映射 - Complex Relationship Mapping using Entity Framework 使用Entity Framework保存AutoMapper映射的实体集合 - Saving AutoMapper mapped Collections of Entities using Entity Framework 将复杂类型映射到实体框架中的SqlQuery - Mapping Complex Types to results form SqlQuery in Entity Framework 使用 AutoMapper 无法 Map 复杂实体框架核心 - Unable to Map Complex Entity Framework Core using AutoMapper AutoMapper和手动映射嵌套的复杂类型 - AutoMapper and manually mapping nested complex types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM