简体   繁体   中英

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. 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:

        
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:

        
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.

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:

[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. Create using 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<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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM