找到未映射的成员(DateTime)

时间:2018-09-28 12:06:28

标签: c# automapper

我有一个包含近100个类的项目,并且正在使用AutoMapper映射数据模型/ DTO。所有数据模型均继承自ZDataBase,所有DTO均继承自ZDTOBase。但是只有6个此类会出现以下错误:

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.
DateTime -> IZDataBase (Destination member list)
System.DateTime -> EasyLOB.Data.IZDataBase (Destination member list)
Unmapped properties:
LookupText

执行以下代码时出现错误:

{
    Console.WriteLine("FornecedorAnexo");
    FornecedorAnexo data = new FornecedorAnexo();
    FornecedorAnexoDTO dto = Mapper.Map<FornecedorAnexoDTO>(data); // <= ERROR
    data = Mapper.Map<FornecedorAnexo>(dto); // <= OK
}    

我不明白错误消息中 DateTime IZDataBase 之间的关系。而且,正如我上面说的,其他所有地图 LookupText 都只有6个类显示此错误。 可能是什么问题?

我使用个人档案创建映射:

public static void SetupMappers()
{
    Mapper.Initialize(cfg => {
        // ZDataModel <-> ZDTOModel
        // EasyDG
        cfg.AddProfile<EasyDGDataAutoMapper>();
    });

    Mapper.Configuration.CompileMappings();

    Mapper.Configuration.AssertConfigurationIsValid();
}

public class EasyDGDataAutoMapper : Profile
{
    public EasyDGDataAutoMapper()
    {
        Assembly dataAssembly = Assembly.GetExecutingAssembly();

        Type[] types = dataAssembly.GetTypes();
        foreach (Type type in types)
        {
            if (type.IsSubclassOf(typeof(ZDataBase)))
            {
                string dto = type.FullName + "DTO";
                Type typeDTO = dataAssembly.GetType(dto);

                CreateMap(type, typeDTO, MemberList.None);
                CreateMap(typeDTO, type, MemberList.None);
            }
        }
    }
} 

在下面找到我的课程:

public abstract class ZDataBase : IZDataBase, INotifyPropertyChanged
{
    [JsonIgnore] // Newtonsoft.Json
    [NotMapped] // MongoDB
    public virtual string LookupText { get; set; }
}

public partial class FornecedorAnexo : ZDataBase
{        
    public virtual int Id { get; set; }
    public virtual int IdFornecedor { get; set; }
    public virtual DateTime Data { get; set; }
    public virtual int IdPessoa { get; set; }
    public virtual string Descricao { get; set; }
    public virtual string FileName { get; set; }
    public virtual string FileAcronym { get; set; }
    public virtual Fornecedor Fornecedor { get; set; } // IdFornecedor
    public virtual Pessoa Pessoa { get; set; } // IdPessoa
}

public abstract class ZDTOBase<TEntityDTO, TEntity> : IZDTOBase<TEntityDTO, TEntity>
    where TEntityDTO : class, IZDTOBase<TEntityDTO, TEntity>
    where TEntity : class, IZDataBase
{
    public virtual string LookupText { get; set; }
}    

public partial class FornecedorAnexoDTO : ZDTOBase<FornecedorAnexoDTO, FornecedorAnexo>
{
    public virtual int Id { get; set; }               
    public virtual int IdFornecedor { get; set; }               
    public virtual DateTime Data { get; set; }               
    public virtual int IdPessoa { get; set; }               
    public virtual string Descricao { get; set; }               
    public virtual string FileName { get; set; }               
    public virtual string FileAcronym { get; set; }
    public virtual string FornecedorLookupText { get; set; } // IdFornecedor
    public virtual string PessoaLookupText { get; set; } // IdPessoa
}

public class EasyDGDataAutoMapper : Profile
{
    public EasyDGDataAutoMapper()
    {
        Assembly dataAssembly = Assembly.GetExecutingAssembly();

        Type[] types = dataAssembly.GetTypes();
        foreach (Type type in types)
        {
            if (type.IsSubclassOf(typeof(ZDataBase)))
            {
                string dto = type.FullName + "DTO";
                Type typeDTO = dataAssembly.GetType(dto);

                CreateMap(type, typeDTO, MemberList.None);
                CreateMap(typeDTO, type, MemberList.None);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在最后一条评论之后,我检查了映射并发现了问题。有一个构造函数,其参数名为 data 。因此,出于任何奇怪的原因,AutoMapper都会将 Data DateTime 属性映射到 data IZDataBase 参数:

var expression = Mapper.Configuration.BuildExecutionPlan(typeof(ClienteAnexo), typeof(ClienteAnexoDTO));
string readableExpression = expression.ToReadableString();
readableExpression += "\r\n";

public ClienteAnexoDTO(IZDataBase data)
{
    FromData(dataModel);
}

为解决该问题,我在配置中加入了 cfg.DisableConstructorMapping();