- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
"-6ren"> "-AutoMapper IQueryable 扩展的 Project().To().SingleOrDefault()抛出这个异常: Cannot compare elements of type 'A-6ren">
AutoMapper IQueryable 扩展的 Project().To<TViewModel>().SingleOrDefault()
抛出这个异常:
Cannot compare elements of type 'App.Domain.MyComplexType. Only primitive types, enumeration types and entity types are supported.
我有这个模型:
public class MyEntityType // this is an entity type on the dbContext
{
public int Id {get;set;
public MyComplexType MyComplexType {get;set;}
}
public class MyComplexType // this is a complex type
{
public decimal Property1 { get; set;}
public string Property2 { get;set;}
}
public class ViewModel
{
public int Id { get;set;}
public decimal MyComplexTypeProperty1 { get;set;}
}
我使用 AutoMapper 配置来自 IQueryable<MyEntityType>
的映射至 ViewModel
:
Mapper.CreateMap<MyEntityType, MyComplexType>(); // I rely on AutoMapper's
//convention for flattening `source.MyComplexType.Property1` to `dest.MyComplexTypeProperty1'
然后我尝试像这样检索单个项目:
var myItem = myContext.Where(x => x.Id == id).Project().To<ViewModel>().SingleOrDefault();
我在 SingleOrDefault()
时得到上述异常被称为,所以显然
我目前通过先调用 SingleOrDefault()
来解决这个问题然后进行映射,这有效:
var myItem = Mapper.Map<ViewModel>(myContext.Find(id));
其他帖子基本上说当尝试将 EF 复杂类型与 null 进行比较时会出现上述错误,例如,在 Where
中条款,但这里显然不是这种情况。
最佳答案
LINQ to entities 无法按照您的建议对复杂类型执行比较(空检查)。例如,这不起作用...
myContext.Select(i => new
{
MyComplexType = i.MyComplexType != null ?
new MyComplexTypeViewModel()
{
Property1 = i.MyComplexType.Property1
}
: null
})
默认情况下,Automapper 会尝试将 null 源值映射为 null,有时会在使用 Project().To<>()
时在生成的表达式中添加类似的条件。或 Mapper.Engine.CreateMapExpression<,>()
.
在我的例子中,我将整个复杂类型映射到它自己的 View 模型并且没有使用属性展平。这个配置值为我解决了这个问题......
Mapper.AllowNullDestinationValues = false;
您可以尝试使用 CreateMapExpression<TSource,TDest>()
手动创建映射表达式并查找对复杂类型的空检查,看看是否是相同的情况。
关于c# - AutoMapper IQueryable Extension 抛出 "Cannot compare elements of type <Complex Type>",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30197233/
我正在尝试在两个对象列表之间进行映射。源类型具有类型为 A 的复杂属性。 ;目标类型是类型 A 的扁平子集加上源类型中的附加标量属性。 public class A { public int
Automapper 是否可以将平面对象映射到复杂的对象图? Mapper.CreateMap() 将 PersonDto.BirthCertificateFatherName 映射到 Person
我有类似 this. 的问题但是这个答案对我不起作用。 我正在做一个小项目。我有两个域模型(Post,Source): public class Post { public Post()
假设我在“消息”类上有一个“评论”属性。我还有 2 个类属性,它们具有“Body”属性。如果该类设置了任一类属性,我希望 AutoMapper 将 Body 属性投影到模型的 comment 属性中,
我是 AutoMapper 的新手,有一个我正在尝试解决的问题。 如果我有这样的源类: public class Membership { public int MembershipId {
我有一个场景,我想忽略基类中定义的类的某些属性。 我有一个这样的初始映射 Mapper.CreateMap() .Include()
如何覆盖 AutoMapper 用于给定属性的类型转换器? 例如,如果我有: public class Foo { public string Name { get; set; } } pub
我正在尝试使用 AutoMapper 将三个实体模型映射到一个 View 模型中。最终输出应该是一个递归类别树,其中包含类别中的产品。类别树正在运行,但 View 模型的 Products 属性为 n
我有一个 Student目的: public class Student { public int Id { get; set; } public string FirstName {
我有一个复杂的对象,如: public class BusinessUnit { public TradingDesk TradingDesk { get; } pub
假设我有这个类: public class Account { public int AccountID { get; set; } public Enterprise Enterpr
从概念上我发现 Automapper 非常有趣。然而,我正试图在上面烧伤(或加热)我的手指。有人可以帮我开始吗?我还不明白我可以从哪里开始。我会喜欢从头开始写一些代码(而不是使用其他人的样本)然后去做
我一直在尝试创建一个自动映射器自定义值解析器,但我似乎错过了一些设置步骤,因为它似乎永远找不到 public abstract class ValueResolver : IValueResolve
我正在尝试让 AutoMapper 为我们在 View 模型上本地化所有 DateTime 属性。我们在系统中的任何地方都使用 UTC 并将所有内容以 UTC 存储在数据库中,但我们希望自动将其转换为
我知道是 AutoMapper而不是 AutoMerge(r),而是…… 我已经开始使用 AutoMapper 并且需要映射 A -> B,并从 C 添加一些属性,以便 B 成为一种 A + C 的平
鉴于这些类,我如何映射它们的字典? public class TestClass { public string Name { get; set; } } public class TestC
我想自定义 AutoMapper 转换类型的方式,而不丢失 AutoMapper 已实现的功能。 我可以创建一个自定义 ITypeConverter实例,但我不知道如何调用默认行为。 Mapper.C
从存储过程返回的数据有 3 列重复数据: Name | Address | PhoneNumber | UniqueCol1 | UniqueCol2 理想情况下,我希望我的模型通过仅存储一次值并收集
当我使用 List 属性映射对象时,默认情况下 Automapper 会将目标对象的 list 属性设置为源对象的实例。 有没有办法让自动映射器创建一个新列表并复制项目但不复制列表实例? 我希望通过以
如果我有这组源类怎么办: namespace Source { class CA { public CB B { get; set; } } class
我是一名优秀的程序员,十分优秀!