- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
考虑以下代码:
protected override IEnumerable<IListModel> GetListInternal(
IQueryModel2<Contact> queryModel) {
/// Causes exception
return this.Query(queryModel).Project().To<ContactListModel>().AsEnumerable();
/// Does not cause exception
return Mapper.Map<IQueryable<Contact>, IEnumerable<ContactListModel>>(this.Query(queryModel)).ToList();
}
当第一行返回时,我得到异常:Operation could destabilize the runtime。当第二行返回时,它工作得很好。据我了解,第二个返回实际上返回一个具体的 IEnumerable<ContactListModel>
,但第一次返回没有。我追加 .AsEnumerable()
没关系, ToArray()
, 或 .ToList()
对于第一次返回,我只是不断收到异常。因此必须有关于什么的东西 Project()
正在返回。无论它返回什么,都可能是 IEnumerable<ContactListModel>
因此传递了方法的返回要求,但它是具体对象,当它在管道中进一步处理时会导致异常。
具体来说,此方法返回的集合被传递到 ListWrapper<TEntity>
做最后处理的类。当我调用 .Count()
时总是抛出异常关于收藏。
我正在拼命寻求解决投影问题的指导,因为第二次返回效率非常低。从 MiniProfiler 告诉有关使用此特定代码的页面的内容来看,我正在进行 1204 个 SQL 查询,其中有重复项,我认为我们所有人都同意这是一个问题......在此先感谢您的任何建议!
这是Query()
和 IQueryModel2<TEntity>
看起来像:
public IQueryable<TEntity> Query(
IQueryModel2<TEntity> queryModel = null) {
return this.QueryInternal(queryModel);
}
private IQueryable<TEntity> QueryInternal(
IQueryModel2<TEntity> queryModel) {
IQueryable<TEntity> entities = this.Context.Set<TEntity>();
if (queryModel != null) {
if (queryModel.Entities != null) {
entities = queryModel.Entities.AsQueryable();
}
if (queryModel.Predicate != null) {
entities = entities.AsExpandable().Where(queryModel.Predicate);
}
}
return entities;
}
public interface IQueryModel2<TEntity> {
IEnumerable<TEntity> Entities { get; set; }
Expression<Func<TEntity, bool>> Predicate { get; set; }
SearchPostModel Search { get; set; }
}
更新(一)
public sealed class ContactListModel :
ListModel {
[Display(Order = 3)]
public string Email { get; set; }
[Display(Order = 1)]
public string Name { get; set; }
[Display(Order = 2)]
public string Phone { get; set; }
[Display(Order = 4)]
public ContactType ContactType { get; set; }
}
internal sealed class ContactToContactListModel :
Profile {
protected override void Configure() {
base.Configure();
base.CreateMap<Contact, ContactListModel>()
.ForMember(
d => d.Name,
o => o.MapFrom(
s => (s.FirstName + " " + s.LastName)))
.IgnoreAllUnmapped();
}
}
更新(二)
public static class AutoMapperExtensions {
public static IMappingExpression<TSource, TDestination> IgnoreAllUnmapped<TSource, TDestination>(
this IMappingExpression<TSource, TDestination> expression) {
if (expression == null) {
return null;
}
return IgnoreAllUnmappedInternal<TSource, TDestination>(expression, typeof(TSource), typeof(TDestination));
}
private static IMappingExpression<TSource, TDestination> IgnoreAllUnmappedInternal<TSource, TDestination>(
IMappingExpression<TSource, TDestination> expression,
Type sourceType,
Type destinationType) {
IEnumerable<string> unmappedProperties = Mapper.GetAllTypeMaps().First(
m =>
m.SourceType.Equals(sourceType)
&& m.DestinationType.Equals(destinationType)).GetUnmappedPropertyNames();
foreach (string unmappedProperty in unmappedProperties) {
expression.ForMember(unmappedProperty, o => o.Ignore());
}
return expression;
}
}
更新 3
因此,我将项目的程序集加载到 LinqPad 中并重新编写了其中的查询代码。感谢惊人的.Dump()
LinqPad 的方法我能够观察到返回的对象到底是什么。所以第一个 return 返回一个 DbQuery<T>
默认情况下,第二个返回 IList<T>
默认情况下。具有讽刺意味的是,当我在 LinqPad 中调用完全相同的代码序列时,它自己不会抛出异常不并且我得到了我期望的 ListWrapper。我真的不确定为什么它在 ASP.NET MVC 中不起作用。虽然我什至不确定这是 ASP.NET 的错,因为它在代码中崩溃了,而代码是另一个程序集中的库项目...
我仍然不知道如何解决这个异常,因为 LinqPad 没有出现这个问题。不确定它是否重要,但我在解决方案的所有项目中都以 .NET 4.5.2 为目标,所以我不确定它是否与此相关?
最佳答案
将 Automapper 升级到 v3.2.0。
这解决了我遇到的确切问题。
关于c# - 在 ASP.NET MVC 中将 AutoMapper QueryableExtensions 与 Entity Framework 一起使用会导致 "Operation could destabilize the runtime"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26172066/
我正在尝试在两个对象列表之间进行映射。源类型具有类型为 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
我是一名优秀的程序员,十分优秀!