c# - ICollection 类型成员的 AutoMapper 上出现 "Error mapping types"错误

标签 c# entity-framework-6 automapper

尝试将 DTO 转换为模型时收到以下错误消息。

MEMBER_Model model = Mapper.Map<MEMBER_Model>(item);

Error mapping types.

Mapping types: T_MEMBER -> MEMBER_Model TL.CFM.DATA.T_MEMBER -> TL.CFM.CORE.MEMBER_Model

Type Map configuration: T_MEMBER -> MEMBER_Model TL.CFM.DATA.T_MEMBER -> TL.CFM.CORE.MEMBER_Model

Destination Member: MEMBER_GROUPs

DTO 类:

public partial class T_MEMBER
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public T_MEMBER()
    {
        this.REL_MEMBER_GROUP = new HashSet<REL_MEMBER_GROUP>();
    }

    public decimal ID { get; set; }
    public string USERNAME { get; set; }
    public string PASSWORD { get; set; }
    public decimal IS_ACTIVE { get; set; }
    public decimal IS_DELETED { get; set; }
    public Nullable<decimal> CRE_BY { get; set; }
    public Nullable<System.DateTime> CRE_DATE { get; set; }
    public Nullable<decimal> UPD_BY { get; set; }
    public Nullable<System.DateTime> UPD_DATE { get; set; }
    public decimal PERSON_ID { get; set; }

}

模型类:

public class MEMBER_Model : _BaseModel
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public MEMBER_Model()
    {
        this.MEMBER_GROUPs = new HashSet<MEMBER_GROUP_Model>();
    }
    public override decimal ID { get; set; }
    public string USERNAME { get; set; }
    public string PASSWORD { get; set; }
    public bool IS_ACTIVE { get; set; }
    public bool IS_DELETED { get; set; }
    public override Nullable<decimal> CRE_BY { get; set; }
    public override Nullable<DateTime> CRE_DATE { get; set; }
    public override Nullable<decimal> UPD_BY { get; set; }
    public override Nullable<DateTime> UPD_DATE { get; set; }
    public decimal PERSON_ID { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<MEMBER_GROUP_Model> MEMBER_GROUPs { get; set; }

    public string Fullname
    {
        get
        {
            return string.Format("{0} {1}", FIRST_NAME, LAST_NAME);
        }
    }
}

自动映射器配置:

public static void SetAutoMapperConfs()
{
    Mapper.Initialize(cfg =>
    {
        #region MEMBER_Model -1
        cfg.CreateMap<T_MEMBER, MEMBER_Model>()
            .ForMember(d => d.MEMBER_GROUPs, f => f.MapFrom(src => src.REL_MEMBER_GROUP))
            .ReverseMap();
        #endregion

        #region MEMBER_GROUP_Model -2
        cfg.CreateMap<REL_MEMBER_GROUP, MEMBER_GROUP_Model>()
            .ForMember(d => d.AUTH_GROUP, f => f.MapFrom(src => src.LKP_AUTH_GROUP))
            .ForMember(d => d.MEMBER, f => f.MapFrom(src => src.T_MEMBER))
            .ReverseMap();
        #endregion      

        #region AUTH_GROUP_Model -3
        cfg.CreateMap<LKP_AUTH_GROUP, AUTH_GROUP_Model>()
            .ForMember(d => d.GROUP_ROLEs, f => f.MapFrom(src => src.REL_GROUP_ROLE))
            .ForMember(d => d.MEMBER_GROUPs, f => f.MapFrom(src => src.REL_MEMBER_GROUP))
            .ReverseMap();
        #endregion

        #region GROUP_ROLE_Model -4
        cfg.CreateMap<REL_GROUP_ROLE, GROUP_ROLE_Model>()
            .ForMember(d => d.AUTH_GROUP, f => f.MapFrom(src => src.LKP_AUTH_GROUP))
            .ForMember(d => d.ROLE, f => f.MapFrom(src => src.LKP_ROLE))
            .ReverseMap();
        #endregion

        #region ROLE_Model -5
        cfg.CreateMap<LKP_ROLE, ROLE_Model>()
            //.ForMember(d => d.GROUP_ROLEs, f => f.MapFrom(src => src.REL_GROUP_ROLE))
            .ReverseMap();
        #endregion
    });
}

Numbers at #region labels showing relation flow.

如您所见,#region ROLE_Model -5 有注释行。这条线导致了问题。 (注意:我认为递归第一次从这里开始,它可能会触发问题)

=更新=

输出值:

Exception thrown: 'AutoMapper.AutoMapperConfigurationException' in AutoMapper.dll frknc: AutoMapper.AutoMapperConfigurationException: Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters

=============================================================

AutoMapper created this type map for you, but your types cannot be mapped using the current configuration. REL_GROUP_ROLE -> MEMBER_GROUP_Model (Destination member list) TL.CFM.DATA.REL_GROUP_ROLE -> TL.CFM.CORE.MEMBER_GROUP_Model (Destination member list)

Unmapped properties: ID MEMBER_ID AUTH_GROUP MEMBER

at AutoMapper.ConfigurationValidator.AssertConfigurationIsValid(IEnumerable`1 typeMaps) Exception thrown: 'AutoMapper.AutoMapperMappingException' in AutoMapper.dll frknc: AutoMapper.AutoMapperMappingException: Error mapping types.

Mapping types: LKP_ROLE -> ROLE_Model TL.CFM.DATA.LKP_ROLE -> TL.CFM.CORE.ROLE_Model

Type Map configuration: LKP_ROLE -> ROLE_Model TL.CFM.DATA.LKP_ROLE -> TL.CFM.CORE.ROLE_Model

Destination Member: GROUP_ROLEs ---> AutoMapper.AutoMapperConfigurationException: Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters

=============================================================

AutoMapper created this type map for you, but your types cannot be mapped using the current configuration. REL_GROUP_ROLE -> MEMBER_GROUP_Model (Destination member list) TL.CFM.DATA.REL_GROUP_ROLE -> TL.CFM.CORE.MEMBER_GROUP_Model (Destination member list)

Unmapped properties: ID MEMBER_ID AUTH_GROUP MEMBER

at AutoMapper.ConfigurationValidator.AssertConfigurationIsValid(IEnumerable`1 typeMaps) at lambda_method(Closure , LKP_ROLE , ROLE_Model , ResolutionContext ) --- End of inner exception stack trace ---
at lambda_method(Closure , LKP_ROLE , ROLE_Model , ResolutionContext ) Exception thrown: 'AutoMapper.AutoMapperMappingException' in AutoMapper.dll frknc: AutoMapper.AutoMapperMappingException: Error mapping types.

Mapping types: REL_GROUP_ROLE -> GROUP_ROLE_Model TL.CFM.DATA.REL_GROUP_ROLE -> TL.CFM.CORE.GROUP_ROLE_Model

Type Map configuration: REL_GROUP_ROLE -> GROUP_ROLE_Model TL.CFM.DATA.REL_GROUP_ROLE -> TL.CFM.CORE.GROUP_ROLE_Model

Destination Member: ROLE ---> AutoMapper.AutoMapperMappingException: Error mapping types.

Mapping types: LKP_ROLE -> ROLE_Model TL.CFM.DATA.LKP_ROLE -> TL.CFM.CORE.ROLE_Model

Type Map configuration: LKP_ROLE -> ROLE_Model TL.CFM.DATA.LKP_ROLE -> TL.CFM.CORE.ROLE_Model

Destination Member: GROUP_ROLEs ---> AutoMapper.AutoMapperConfigurationException: Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters

=============================================================

AutoMapper created this type map for you, but your types cannot be mapped using the current configuration. REL_GROUP_ROLE -> MEMBER_GROUP_Model (Destination member list) TL.CFM.DATA.REL_GROUP_ROLE -> TL.CFM.CORE.MEMBER_GROUP_Model (Destination member list)

Unmapped properties: ID MEMBER_ID AUTH_GROUP MEMBER

at AutoMapper.ConfigurationValidator.AssertConfigurationIsValid(IEnumerable`1 typeMaps) at lambda_method(Closure , LKP_ROLE , ROLE_Model , ResolutionContext ) --- End of inner exception stack trace ---
at lambda_method(Closure , LKP_ROLE , ROLE_Model , ResolutionContext ) at AutoMapper.Mapper.AutoMapper.IRuntimeMapper.Map[TSource,TDestination](TSource source, TDestination destination, ResolutionContext context, IMemberMap memberMap) at lambda_method(Closure , LKP_AUTH_GROUP , AUTH_GROUP_Model , ResolutionContext ) --- End of inner exception stack trace --- at lambda_method(Closure , LKP_AUTH_GROUP , AUTH_GROUP_Model , ResolutionContext ) Exception thrown: 'AutoMapper.AutoMapperMappingException' in AutoMapper.dll frknc: AutoMapper.AutoMapperMappingException: Error mapping types.

Mapping types: LKP_AUTH_GROUP -> AUTH_GROUP_Model TL.CFM.DATA.LKP_AUTH_GROUP -> TL.CFM.CORE.AUTH_GROUP_Model

Type Map configuration: LKP_AUTH_GROUP -> AUTH_GROUP_Model TL.CFM.DATA.LKP_AUTH_GROUP -> TL.CFM.CORE.AUTH_GROUP_Model

Destination Member: GROUP_ROLEs ---> AutoMapper.AutoMapperMappingException: Error mapping types.

Mapping types: REL_GROUP_ROLE -> GROUP_ROLE_Model TL.CFM.DATA.REL_GROUP_ROLE -> TL.CFM.CORE.GROUP_ROLE_Model

Type Map configuration: REL_GROUP_ROLE -> GROUP_ROLE_Model TL.CFM.DATA.REL_GROUP_ROLE -> TL.CFM.CORE.GROUP_ROLE_Model

Destination Member: ROLE ---> AutoMapper.AutoMapperMappingException: Error mapping types.

Mapping types: LKP_ROLE -> ROLE_Model TL.CFM.DATA.LKP_ROLE -> TL.CFM.CORE.ROLE_Model

Type Map configuration: LKP_ROLE -> ROLE_Model TL.CFM.DATA.LKP_ROLE -> TL.CFM.CORE.ROLE_Model

Destination Member: GROUP_ROLEs ---> AutoMapper.AutoMapperConfigurationException: Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters

=============================================================

AutoMapper created this type map for you, but your types cannot be mapped using the current configuration. REL_GROUP_ROLE -> MEMBER_GROUP_Model (Destination member list) TL.CFM.DATA.REL_GROUP_ROLE -> TL.CFM.CORE.MEMBER_GROUP_Model (Destination member list)

Unmapped properties: ID MEMBER_ID AUTH_GROUP MEMBER

at AutoMapper.ConfigurationValidator.AssertConfigurationIsValid(IEnumerable`1 typeMaps) at lambda_method(Closure , LKP_ROLE , ROLE_Model , ResolutionContext ) --- End of inner exception stack trace ---
at lambda_method(Closure , LKP_ROLE , ROLE_Model , ResolutionContext ) at AutoMapper.Mapper.AutoMapper.IRuntimeMapper.Map[TSource,TDestination](TSource source, TDestination destination, ResolutionContext context, IMemberMap memberMap) at lambda_method(Closure , LKP_AUTH_GROUP , AUTH_GROUP_Model , ResolutionContext ) --- End of inner exception stack trace --- at lambda_method(Closure , LKP_AUTH_GROUP , AUTH_GROUP_Model , ResolutionContext ) --- End of inner exception stack trace --- at lambda_method(Closure , LKP_AUTH_GROUP , AUTH_GROUP_Model , ResolutionContext ) Exception thrown: 'AutoMapper.AutoMapperMappingException' in AutoMapper.dll

最佳答案

您是否尝试在配置文件中忽略它们并查看错误是否仍然存在?如果它仍然存在,您是否尝试查看异常详细信息?应该说哪些成员没有被映射? ForAllOtherMemebers(x=>x.Ignore())。更多信息:dotnettutorials.net/lesson/ignore-using-automapper-in-csharp

最重要的是,您无法将 ICollection 映射到 ICollection,因为它们不是具体类型。例如:

源可以是 IEnumerable,但输出只能是 List。

关于c# - ICollection 类型成员的 AutoMapper 上出现 "Error mapping types"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56111298/

相关文章:

c# - 淡出一个窗口

c# - 如何使用 Entity Framework 6 代码优先方法在 SQL Server 中创建序列并应用两个表?

c# - SQLException : Cannot insert duplicate key row in object 'dbo.Users' with unique index

mysql - 创建索引时,带有 mysql 数据库迁移的 Entity Framework 失败

asp.net-mvc-2 - ASP.NET MVC 2 自动映射器放置

c# - 从 Linq 查询调用方法

c# - 如何在 JSON.NET 中使用 List<T> 属性进行序列化期间将 null JSON 属性默认为空数组?

c# - 对基于 LINQ 的列表使用匿名类型而不是 var

asp.net - 如何调试成 AutoMapper 代码?

c# - 如何使用 AutoMapper 将 POCO 映射到 Record