c# - 找到未映射的成员(即使忽略该属性)

标签 c# automapper

我正在努力解决这里的问题。我在整个项目中都使用了 AutoMapper,但从未遇到过这个具体问题。它提示一个未映射的属性,但我已经告诉它忽略该属性,但它仍然给出错误。

这是我从 (UserDefinedFieldViewModel) 映射的模型:

public class UserDefinedFieldViewModel
{
    public string Id { get; set; }
    public UsedFor UsedFor { get; set; }
    public UserDefinedFieldDataType DataType { get; set; }
    public DataTypeDisplay
    {
        get
        {
            switch (DataType)
            {
                case UserDefinedFieldDataType.ShortText:
                    return "Short Text";
                case UserDefinedFieldDataType.LongText:
                    return "Long Text";
                case UserDefinedFieldDataType.Date:
                    return "Date";
                case UserDefinedFieldDataType.Number:
                    return "Number";
                case UserDefinedFieldDataType.Boolean:
                    return "True/False";
                default:
                    return "Unknown";
            }
        }
    }
    public string FieldName { get; set; }
    public string HelpText { get; set; }
}

这是我映射到的模型(UserDefinedField):

public class UserDefinedField
{
    public string Id       { get; set; }
    public int OrgId       { get; set; }
    public UsedFor UsedFor { get; set; }
    public UserDefinedFieldDataType DataType { get; set; }
    public string FieldName     { get; set; }
    public string HelpText      { get; set; }
    public bool IsDeleted       { get; set; }
    public string CreateById    { get; set; }
    public DateTime CreateDate  { get; set; }
    public string LastModById   { get; set; }
    public DateTime LastModDate { get; set; }
}

我得到的确切错误是:

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

UserDefinedFieldViewModel -> UserDefinedField (Destination member list) Web.Areas.Admin.ViewModels.CustomFields.UserDefinedFieldViewModel -> Models.Core.UserDefinedField (Destination member list)

Unmapped properties: OrgId

我只是不确定是什么导致了这里的问题。我已经在整个项目中完成了这种类型的映射(包括仅将 OrgId 放在实际模型上)而没有出现此错误。

我尝试忽略 OrgId 以查看是否可行,但它似乎也没有效果。这是我的映射当前的样子:

x.CreateMap<UserDefinedFieldViewModel, UserDefinedField>().ForMember(dest => dest.OrgId, options => options.Ignore()).ReverseMap();

最佳答案

我今天遇到了同样的错误。最后我发现我的映射器初始化不正确。如果您使用的是 .Net Core,则需要创建一个继承自 Automapper.Profile 的配置文件类,并在其构造函数中添加所有映射器配置。请参阅这篇文章了解更多详情:

https://medium.com/ps-its-huuti/how-to-get-started-with-automapper-and-asp-net-core-2-ecac60ef523f

关于c# - 找到未映射的成员(即使忽略该属性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51524263/

相关文章:

c# - 在远程计算机上使用与 Excel 的互操作

c# - 将文本绑定(bind)到组合框中的选定项目

c# - SDN Entity Framework 控制台演示无法编译

c# - 如何在 AutoMapper 中全局使用忽略?

c# - 用于将匿名类型投影到 View 模型的自动映射器查询

C# Automapper 如何使用 customresolver 中的属性进行解析

c# - 为什么我的测试 "propType == typeof(ObservableCollection<string>)"失败了?

c# - 如何根据不同类型的用户隐藏、启用、更改 WPF GUI?

c# - 我应该在哪里放置自动映射器代码?

asp.net-mvc - MVCScaffolding 和 AutoMapper 映射的 ViewModel