Не найденные участники были найдены (DateTime) - PullRequest
0 голосов
/ 28 сентября 2018

У меня есть проект с почти 100 классами, и я сопоставляю модели данных / DTO с помощью AutoMapper.Все модели данных унаследованы от 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 в сообщении об ошибке.И, как я уже говорил выше, только 6 классов показывают эту ошибку, все остальные карты LookupText .В чем может быть проблема?

Я создаю отображение, используя Профили:

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 голосов
/ 29 сентября 2018

После последнего комментария я проверил отображение и обнаружил проблему.Существует конструктор с параметром с именем 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 (); в моей конфигурации.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...