自动映射器说缺少从System.String到System.Char的映射?但我看不到Char属性 [英] Automapper says Missing map from System.String to System.Char? But I see no Char property
问题描述
我有三节课:
public class UserReport : Entity
{
public string Name { get; set; }
public string Email { get; set; }
public List<string> Departments { get; set; }
public List<string> Titles { get; set; }
}
public abstract class Entity
{
public Guid Id { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateLastModified { get; set; }
public string CreatedBy { get; set; }
public string LastModifiedBy { get; set; }
public bool Active { get; set; }
protected Entity()
{
DateCreated = DateTime.Now;
DateLastModified = DateTime.Now;
Id = Guid.NewGuid();
}
}
public class UserModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Departments { get; set; }
public string Titles { get; set; }
}
将我的自动映射器配置设置为:
With my automapper configs set as:
CreateMap<List<string>, string>().ConvertUsing(strings => {
if (strings != null)
return string.Join("\n", strings);
return "";
});
CreateMap<UserReport, UserModel>();
当尝试使用Automapper Ef Extensions从通用方法进行调用时:
When trying to call from a generic method using the Automapper Ef Extensions:
IQueryable<TModel> modelQueryable = _reportService.GetReportBaseQuery().ProjectToQueryable<TModel>();
我收到此错误
缺少从System.String到System.Char的映射.使用Mapper.CreateMap创建.
Missing map from System.String to System.Char. Create using Mapper.CreateMap.
GetReportBaseQuery()
返回IQueryable<TReport>
,因此在这种情况下为UserReport
.我没有看到任何char
属性,为什么会出现这种情况?
GetReportBaseQuery()
returns an IQueryable<TReport>
, so the UserReport
in this instance. I don't see any char
properties, why is this coming up?
仅出于测试目的,我尝试制作一个:
Just for testing I tried to make one:
CreateMap<String, Char>().ConvertUsing(x => x.FirstOrDefault());
然后它说:
参数类型不匹配
Argument types do not match
进一步的研究表明,这种方法有效:
Further research shows that this works:
Mapper.Map<List<TReport>, List<TModel>>(_reportService.GetReportBaseQuery().ToList());
但是我不能使用它,因为我需要它成为可查询的返回值.因此,当我尝试进行EF投影时,情况有所不同,但不确定是什么.将选择语句从一个语句编写到另一个语句很容易,但这不是通用的并且不可重用.
But I can't use that since I need it to be a queryable returned. So something is different when I try to do an EF projection, not sure what that is though. Writing a select statement from one to the other is easy enough, but that's not generic and re-usable.
推荐答案
解决方案是显式设置Departments
和Titles
字段的映射:
The solution is to explicitly set mapping for the Departments
and Titles
fields:
CreateMap<UserReport, UserModel>()
.ForMember(x => x.Departments, o => o.MapFrom(s => string.Join("\n", s.Departments)))
.ForMember(x => x.Titles, o => o.MapFrom(s => string.Join("\n", s.Titles)));
但是,这并不能解释为什么会发生这种情况. 正如DOTang指出的那样:
But, this does not explain, why this situation occurs. As DOTang pointed out:
LINQ to Entities无法识别方法"System.String Join(System.String,System.Collections.Generic.IEnumerable1 [System.String])"方法,并且该方法无法转换为商店表达式.
LINQ to Entities does not recognize the method 'System.String Join(System.String, System.Collections.Generic.IEnumerable1[System.String])' method, and this method cannot be translated into a store expression.
AutoMapper扩展似乎试图映射以下内容:
The AutoMapper extension seems to try to map the following thing:
IEnumerable<string> => IEnumerable<char>
这篇关于自动映射器说缺少从System.String到System.Char的映射?但我看不到Char属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!