简体   繁体   English

Automapper 错误:System.InvalidOperationException:'缺少从 System.String 到 System.Char 的 map

[英]Automapper error: System.InvalidOperationException: 'Missing map from System.String to System.Char

I need to use automapper to populate the address fields in CompanyVM from a Company model.我需要使用 automapper 从 Company model 填充 CompanyVM 中的地址字段。 A Company may have many Addresses.一家公司可能有许多地址。 Preferably I want an Active address that is also Primary.最好我想要一个也是主要的活动地址。 Failing that, I want simply an Active Address.如果做不到这一点,我只想要一个活动地址。 Failing that, just give me an address.不行就给个地址吧

Here is the Company model:这是公司 model:

        
public class Company
    {
        public int CompanyID { get; set; }        
        public string Name { get; set; }
        public string Type { get; set; }
        public string Industry { get; set; }
        public string DBA { get; set; }
        public string TaxID { get; set; }
        public bool Active { get; set; } = true;
        public virtual ICollection<Address> Addresses { get; set; } = new List<Address>();

Here is the Address model:这是地址 model:

        
public class Address
    {        
        public int AddressID { get; set; }
        public string Type { get; set; }
        public string Street1 { get; set; }
        public string Street2 { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }
        public string County { get; set; }
        public string Country { get; set; }
        public bool Primary { get; set; } = false;
        public bool Active { get; set; } = true;
        public int? CompanyCompanyID { get; set; }

I will need to map many of these but just trying to get it to work for Street1 now.我将需要 map 其中许多,但现在只是想让它为 Street1 工作。

Here are the automapper statements I'm trying to use:以下是我尝试使用的自动映射器语句:

        public class CompanyProfile : Profile
    {
        public CompanyProfile()
        {
            CreateMap<Company, CompanyVM>()
                .ForMember(vm => vm.Street, opt => opt.MapFrom(model =>
                    model.Addresses.Where(x => x.CompanyCompanyID != null && x.Active && x.Primary).Select(x => x.Street1) ??
                    model.Addresses.Where(x => x.CompanyCompanyID != null && x.Active).Select(x => x.Street1) ??
                    model.Addresses.Where(x => x.CompanyCompanyID != null).Select(x => x.Street1)))

                .ReverseMap();              
        }
    }

Here is CompanyVM:这是 CompanyVM:

[Grid2(nameof(CompanyID),
        FavoritesEnabled = true,
        RecentRecordAction = "Details",
        FetchDataOnLoad = true
        )]
    public class CompanyVM : GridRecordVM
    {
        public virtual int CompanyID { get; set; }

        [GridColumn2(TemplateType = GridTemplateType.DefaultDetailLink,
            IncludeInGeneralSearch = true)]
        public virtual string Name { get; set; }
        
        [GridColumn2]
        public virtual string Type { get; set; }
        
        [LockedFilter(FilterOperator.IsEqualTo, true)]
        public virtual bool Active { get; set; }

        [Display(Name = "Industry")]
        [MaxLength(20, ErrorMessage = "Industry cannot be longer that 20 characters.")]
        public string Industry { get; set; }

        [Display(Name = "DBA")]
        [MaxLength(50, ErrorMessage = "DBA name cannot be longer that 50 characters.")]
        public string DBA { get; set; }

        [Display(Name = "Tax Id")]
        [MaxLength(50, ErrorMessage = "TaxID name cannot be longer that 50 characters.")]
        public string TaxID { get; set; }
        
        [Display(Name = "Street", Description = "Street")]      
        public string Street { get; set; }

        //these items set to ignoremap until mapping sorted out
        [Display(Name = "City", Description = "City")]
        [IgnoreMap]
        public string City { get; set; }

        [Display(Name = "State", Description = "State")]
        [IgnoreMap]
        public string State { get; set; }

        [Display(Name = "ZipCode", Description = "ZipCode")]
        [IgnoreMap]
        public string ZipCode { get; set; }

        [Phone]
        [Display(Name = "Phone Number", Description = "Phone Number")]
        [IgnoreMap]
        public string PhoneNumber { get; set; }

        [EmailAddress]
        [Display(Name = "Email Address", Description = "Email Address")]
        [IgnoreMap]
        public string EmailAddress { get; set; }
    }

A runtime error is thrown: System.InvalidOperationException: 'Missing map from System.String to System.Char.引发运行时错误:System.InvalidOperationException:'缺少从 System.String 到 System.Char 的 map。 Create using CreateMap<String, Char>.'使用 CreateMap<String, Char> 创建。 I've seen numerous postings of this issue and have read through many of them but it's still not making sense for me.我已经看过很多关于这个问题的帖子,并且已经阅读了其中的许多帖子,但这对我来说仍然没有意义。 My source and destination fields are both strings.我的源和目标字段都是字符串。 What am I doing wrong?我究竟做错了什么?

Solution was to format the CreateMap.ForMember statement as follows:解决方案是将 CreateMap.ForMember 语句格式化如下:

CreateMap<Company, CompanyVM>()
                .ForMember(x => x.Street, opt => opt.MapFrom(src =>
                    src.Addresses.Where(x => x.Active && x.Primary).FirstOrDefault() != null
                        ? src.Addresses.Where(x => x.Active && x.Primary).FirstOrDefault().Street1
                        : src.Addresses.Where(x => x.Active).FirstOrDefault() != null
                            ? src.Addresses.Where(x => x.Active).FirstOrDefault().Street1
                            : src.Addresses.FirstOrDefault() != null
                                ? src.Addresses.FirstOrDefault().Street1
                                : string.Empty))

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

相关问题 “缺少从System.Char到System.String的映射”AutoMapper错误 - “Missing map from System.Char to System.String” AutoMapper error Automapper说缺少从System.String到System.Char的映射? 但我看不到Char属性 - Automapper says Missing map from System.String to System.Char? But I see no Char property System.InvalidOperationException:缺少参数 - System.InvalidOperationException: Missing parameter DropDownList 错误,System.InvalidOperationException - DropDownList Error, System.InvalidOperationException System.InvalidOperationException 错误原因? - System.InvalidOperationException error reason? C#成员&#39;System.Char [] System.String :: ToCharArray()&#39;在另一个模块中声明,需要导入到Mono.Cecil中 - C# Member 'System.Char[] System.String::ToCharArray()' is declared in another module and needs to be imported in Mono.Cecil System.InvalidOperationException 保存 - System.InvalidOperationException on saving Nunit System.InvalidOperationException - Nunit System.InvalidOperationException System.InvalidOperationException Android - System.InvalidOperationException Android HttpResponseMessage System.InvalidOperationException - HttpResponseMessage System.InvalidOperationException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM