问题描述
我继承了作为单用户开发的 c# winforms 应用程序。它使用 EF 6 DB First、AutoMapper 4.2.1、Generic Repository 和 Uow。当使用本地数据库时,应用程序表现得足够好,但是当使用远程数据库(到服务器的 vpn 连接)时,应用程序非常慢,特别是由于某些使用 AutoMapper 的调用。这个问题也在以下问题中描述:[https://stackoverflow.com/questions/66706496/entity-framework-6-generic-repository-with-automapper-slow-performance-when-runn][1]>
如 [2]:https://stackoverflow.com/users/1663001/davidg 建议应用程序运行缓慢的原因是因为我正在从数据库下载整个用户列表并在内存中进行过滤。
public List<T1> Find(Expression<Func<T1,bool>> predicate)
{
// Note this is still an IQueryable<T> so nothing is executed against the database yet
var query = _dbContext.Set<T>()
.Where(q => !q.IsDeleted)
.Where(predicate);
return AutoMapperConfiguration.GetMapperConfiguration()
.Map<List<T1>>(query);
}
这里的主要问题是我不能在查询 where 子句中应用谓词,因为它应该是 Expression 所以我正在寻找一种方法将 Expression UPDATE,这是Automapper映射器配置类 编辑我在我的项目中添加了 Automapper 的整个配置: AutoMapperConfiguration.cs 忽略VirtualExtensions.cs MapperProfile.cs 和 PrjUsers_viewmodel ................public static class AutoMapperConfiguration
{
private static IMapper mapperConfiguration;
public static IMapper GetMapperConfiguration()
{
return mapperConfiguration;
}
public static void SetMapperConfiguration(IMapper value)
{
mapperConfiguration = value;
}
public static void Configure()
{
var config = new MapperConfiguration(cfg =>
cfg.AddProfile(new MapperProfile()));
SetMapperConfiguration(config.CreateMapper());
}
}
}
using AutoMapper;
public static class AutoMapperConfiguration
{
private static IMapper mapperConfiguration;
public static IMapper GetMapperConfiguration()
{
return mapperConfiguration;
}
public static void SetMapperConfiguration(IMapper value)
{
mapperConfiguration = value;
}
public static void Configure()
{
var config = new MapperConfiguration(cfg =>
cfg.AddProfile(new MapperProfile()));
SetMapperConfiguration(config.CreateMapper());
}
}
public static IMappingExpression<TSource,TDestination>
IgnoreAllVirtual<TSource,TDestination>(
this IMappingExpression<TSource,TDestination> expression)
{
var desType = typeof(TDestination);
foreach (var property in desType.GetProperties().Where(p =>
p.Getgetmethod().IsVirtual && !p.Getgetmethod().IsFinal))
{
expression.ForMember(property.Name,opt => opt.Ignore());
}
return expression;
}
}
internal class MapperProfile : AutoMapper.Profile
{
protected override void Configure()
{
.......................
CreateMap<PrjUser,PrjUser_viewmodel>().ForMember(dest => dest.ConfigSets_LockedBy,opt => opt.MapFrom(src => src.ConfigSets1))
.ForMember(dest => dest.ConfigSet_Attribute_LockedBy,opt => opt.MapFrom(src => src.ConfigSet_Attribute1))
.ForMember(dest => dest.ConfigSet_DataType_Mapping_LockedBy,opt => opt.MapFrom(src => src.ConfigSet_DataType_Mapping1))
.ForMember(dest => dest.ConfigSet_IODPEntity_Mapping_LockedBy,opt => opt.MapFrom(src => src.ConfigSet_IODPEntity_Mapping1))
.ForMember(dest => dest.DataTypes_LockedBy,opt => opt.MapFrom(src => src.DataTypes1))
.ForMember(dest => dest.Features_LockedBy,opt => opt.MapFrom(src => src.Features1))
.ForMember(dest => dest.GeneralParameters_LockedBy,opt => opt.MapFrom(src => src.GeneralParameters1))
.ForMember(dest => dest.HwVariants_LockedBy,opt => opt.MapFrom(src => src.HwVariants1))
.ForMember(dest => dest.HwVariantConfigurationMappings_LockedBy,opt => opt.MapFrom(src => src.HwVariantConfigurationMappings1))
.ForMember(dest => dest.IODPEntities_LockedBy,opt => opt.MapFrom(src => src.IODPEntities1))
.ForMember(dest => dest.MainFunctions_LockedBy,opt => opt.MapFrom(src => src.MainFunctions1))
.ForMember(dest => dest.MainFunction_Attribute_LockedBy,opt => opt.MapFrom(src => src.MainFunction_Attribute1))
.ForMember(dest => dest.Modules_LockedBy,opt => opt.MapFrom(src => src.Modules1))
.ForMember(dest => dest.Module_Feature_Mapping_LockedBy,opt => opt.MapFrom(src => src.Module_Feature_Mapping1))
.ForMember(dest => dest.ModuleEntities_LockedBy,opt => opt.MapFrom(src => src.ModuleEntities1))
.ForMember(dest => dest.ModuleEntity_Association_LockedBy,opt => opt.MapFrom(src => src.ModuleEntity_Association1))
.ForMember(dest => dest.ModuleEntity_Attribute_LockedBy,opt => opt.MapFrom(src => src.ModuleEntity_Attribute1))
.ForMember(dest => dest.ModuleEntity_DataType_Mapping_LockedBy,opt => opt.MapFrom(src => src.ModuleEntity_DataType_Mapping1))
.ForMember(dest => dest.ModuleEntity_HwVariant_Mapping_LockedBy,opt => opt.MapFrom(src => src.ModuleEntity_HwVariant_Mapping1))
.ForMember(dest => dest.ModuleEntity_IODPEntity_Mapping_LockedBy,opt => opt.MapFrom(src => src.ModuleEntity_IODPEntity_Mapping1))
.ForMember(dest => dest.ModuleEntityHwVariant_Attribute_LockedBy,opt => opt.MapFrom(src => src.ModuleEntityHwVariant_Attribute1))
.ForMember(dest => dest.ModuleEntityHwVariant_ConfigSet_Mapping_LockedBy,opt => opt.MapFrom(src => src.ModuleEntityHwVariant_ConfigSet_Mapping1))
.ForMember(dest => dest.ModuleEntityHwVariant_MainFunction_Mapping_LockedBy,opt => opt.MapFrom(src => src.ModuleEntityHwVariant_MainFunction_Mapping1))
.ForMember(dest => dest.ModuleEntityHwVariant_Signal_Mapping_LockedBy,opt => opt.MapFrom(src => src.ModuleEntityHwVariant_Signal_Mapping1))
.ForMember(dest => dest.ModuleEntityHwVariantSignal_Attribute_LockedBy,opt => opt.MapFrom(src => src.ModuleEntityHwVariantSignal_Attribute1))
.ForMember(dest => dest.ModuleEntityHwVariantSignal_Plausibility_Mapping_LockedBy,opt => opt.MapFrom(src => src.ModuleEntityHwVariantSignal_Plausibility_Mapping1))
.ForMember(dest => dest.ModuleProperty_Attribute_LockedBy,opt => opt.MapFrom(src => src.ModuleProperty_Attribute1))
.ForMember(dest => dest.Plausibilities_LockedBy,opt => opt.MapFrom(src => src.Plausibilities1))
.ForMember(dest => dest.Plausibility_Attribute_LockedBy,opt => opt.MapFrom(src => src.Plausibility_Attribute1))
.ForMember(dest => dest.Preferences_LockedBy,opt => opt.MapFrom(src => src.Preferences1))
.ForMember(dest => dest.Signal_Attribute_LockedBy,opt => opt.MapFrom(src => src.Signal_Attribute1))
.ForMember(dest => dest.Signal_DataType_Mapping_LockedBy,opt => opt.MapFrom(src => src.Signal_DataType_Mapping1))
.ForMember(dest => dest.Signal_IODPEntity_Mapping_LockedBy,opt => opt.MapFrom(src => src.Signal_IODPEntity_Mapping1))
.ForMember(dest => dest.Signal_ModuleEntity_Mapping_LockedBy,opt => opt.MapFrom(src => src.Signal_ModuleEntity_Mapping1))
.ForMember(dest => dest.Signals_LockedBy,opt => opt.MapFrom(src => src.Signals1))
.ForMember(dest => dest.Types_LockedBy,opt => opt.MapFrom(src => src.Types1))
.ForMember(dest => dest.ValidValues_LockedBy,opt => opt.MapFrom(src => src.ValidValues1));
CreateMap<PrjUser,PrjUser_viewmodel>().ReverseMap().ForMember(dest => dest.ConfigSet_Attribute1,opt => opt.MapFrom(src => src.ConfigSets_LockedBy))
.ForMember(dest => dest.ConfigSet_DataType_Mapping1,opt => opt.MapFrom(src => src.ConfigSet_Attribute_LockedBy))
.ForMember(dest => dest.ConfigSet_IODPEntity_Mapping1,opt => opt.MapFrom(src => src.ConfigSet_DataType_Mapping_LockedBy))
.ForMember(dest => dest.DataTypes1,opt => opt.MapFrom(src => src.ConfigSet_IODPEntity_Mapping_LockedBy))
.ForMember(dest => dest.Features1,opt => opt.MapFrom(src => src.DataTypes_LockedBy))
.ForMember(dest => dest.GeneralParameters1,opt => opt.MapFrom(src => src.Features_LockedBy))
.ForMember(dest => dest.HwVariants1,opt => opt.MapFrom(src => src.GeneralParameters_LockedBy))
.ForMember(dest => dest.HwVariantConfigurationMappings1,opt => opt.MapFrom(src => src.HwVariantConfigurationMappings_LockedBy))
.ForMember(dest => dest.IODPEntities1,opt => opt.MapFrom(src => src.IODPEntities_LockedBy))
.ForMember(dest => dest.MainFunctions1,opt => opt.MapFrom(src => src.MainFunctions_LockedBy))
.ForMember(dest => dest.MainFunction_Attribute1,opt => opt.MapFrom(src => src.MainFunction_Attribute_LockedBy))
.ForMember(dest => dest.Modules1,opt => opt.MapFrom(src => src.Modules_LockedBy))
.ForMember(dest => dest.Module_Feature_Mapping1,opt => opt.MapFrom(src => src.Module_Feature_Mapping_LockedBy))
.ForMember(dest => dest.ModuleEntities1,opt => opt.MapFrom(src => src.ModuleEntities_LockedBy))
.ForMember(dest => dest.ModuleEntity_Association1,opt => opt.MapFrom(src => src.ModuleEntity_Association_LockedBy))
.ForMember(dest => dest.ModuleEntity_Attribute1,opt => opt.MapFrom(src => src.ModuleEntity_Attribute_LockedBy))
.ForMember(dest => dest.ModuleEntity_DataType_Mapping1,opt => opt.MapFrom(src => src.ModuleEntity_DataType_Mapping_LockedBy))
.ForMember(dest => dest.ModuleEntity_HwVariant_Mapping1,opt => opt.MapFrom(src => src.ModuleEntity_HwVariant_Mapping_LockedBy))
.ForMember(dest => dest.ModuleEntity_IODPEntity_Mapping1,opt => opt.MapFrom(src => src.ModuleEntity_IODPEntity_Mapping_LockedBy))
.ForMember(dest => dest.ModuleEntityHwVariant_Attribute1,opt => opt.MapFrom(src => src.ModuleEntityHwVariant_Attribute_LockedBy))
.ForMember(dest => dest.ModuleEntityHwVariant_ConfigSet_Mapping1,opt => opt.MapFrom(src => src.ModuleEntityHwVariant_ConfigSet_Mapping_LockedBy))
.ForMember(dest => dest.ModuleEntityHwVariant_MainFunction_Mapping1,opt => opt.MapFrom(src => src.ModuleEntityHwVariant_MainFunction_Mapping_LockedBy))
.ForMember(dest => dest.ModuleEntityHwVariant_Signal_Mapping1,opt => opt.MapFrom(src => src.ModuleEntityHwVariant_Signal_Mapping_LockedBy))
.ForMember(dest => dest.ModuleEntityHwVariantSignal_Attribute1,opt => opt.MapFrom(src => src.ModuleEntityHwVariantSignal_Attribute_LockedBy))
.ForMember(dest => dest.ModuleEntityHwVariantSignal_Plausibility_Mapping1,opt => opt.MapFrom(src => src.ModuleEntityHwVariantSignal_Plausibility_Mapping_LockedBy))
.ForMember(dest => dest.ModuleProperty_Attribute1,opt => opt.MapFrom(src => src.ModuleProperty_Attribute_LockedBy))
.ForMember(dest => dest.Plausibilities1,opt => opt.MapFrom(src => src.Plausibilities_LockedBy))
.ForMember(dest => dest.Plausibility_Attribute1,opt => opt.MapFrom(src => src.Plausibility_Attribute_LockedBy))
.ForMember(dest => dest.Preferences1,opt => opt.MapFrom(src => src.Preferences_LockedBy))
.ForMember(dest => dest.Signal_Attribute1,opt => opt.MapFrom(src => src.Signal_Attribute_LockedBy))
.ForMember(dest => dest.Signal_DataType_Mapping1,opt => opt.MapFrom(src => src.Signal_DataType_Mapping_LockedBy))
.ForMember(dest => dest.Signal_IODPEntity_Mapping1,opt => opt.MapFrom(src => src.Signal_IODPEntity_Mapping_LockedBy))
.ForMember(dest => dest.Signal_ModuleEntity_Mapping1,opt => opt.MapFrom(src => src.Signal_ModuleEntity_Mapping_LockedBy))
.ForMember(dest => dest.Signals1,opt => opt.MapFrom(src => src.Signals_LockedBy))
.ForMember(dest => dest.Types1,opt => opt.MapFrom(src => src.Types_LockedBy))
.ForMember(dest => dest.ValidValues1,opt => opt.MapFrom(src => src.ValidValues_LockedBy));
..................................
}
}
public class PrjUser_viewmodel : IEntity
{
public PrjUser_viewmodel()
{
this.ConfigSets = new HashSet<ConfigSet_viewmodel>();
this.ConfigSet_Attribute = new HashSet<ConfigSet_Attribute_viewmodel>();
this.ConfigSet_DataType_Mapping = new HashSet<ConfigSet_DataType_Mapping_viewmodel>();
this.ConfigSet_IODPEntity_Mapping = new HashSet<ConfigSet_IODPEntity_Mapping_viewmodel>();
this.DataTypes = new HashSet<DataType_viewmodel>();
this.Features = new HashSet<Feature_viewmodel>();
this.GeneralParameters = new HashSet<GeneralParameter_viewmodel>();
this.HwVariants = new HashSet<HwVariant_viewmodel>();
this.HwVariants_LockedBy = new HashSet<HwVariant_viewmodel>();
this.IODPEntities = new HashSet<IODPEntity_viewmodel>();
this.MainFunctions = new HashSet<MainFunction_viewmodel>();
this.MainFunction_Attribute = new HashSet<MainFunction_Attribute_viewmodel>();
this.Modules = new HashSet<Module_viewmodel>();
this.Module_Feature_Mapping = new HashSet<Module_Feature_Mapping_viewmodel>();
this.ModuleEntities = new HashSet<ModuleEntity_viewmodel>();
this.ModuleEntity_Association = new HashSet<ModuleEntity_Association_viewmodel>();
this.ModuleEntity_Attribute = new HashSet<ModuleEntity_Attribute_viewmodel>();
this.ModuleEntity_HwVariant_Mapping = new HashSet<ModuleEntity_HwVariant_Mapping_viewmodel>();
this.ModuleEntity_DataType_Mapping = new HashSet<ModuleEntity_DataType_Mapping_viewmodel>();
this.ModuleEntity_IODPEntity_Mapping = new HashSet<ModuleEntity_IODPEntity_Mapping_viewmodel>();
this.ModuleEntityHwVariant_Attribute = new HashSet<ModuleEntityHwVariant_Attribute_viewmodel>();
this.ModuleEntityHwVariant_ConfigSet_Mapping = new HashSet<ModuleEntityHwVariant_ConfigSet_Mapping_viewmodel>();
this.ModuleEntityHwVariant_MainFunction_Mapping = new HashSet<ModuleEntityHwVariant_MainFunction_Mapping_viewmodel>();
this.ModuleEntityHwVariant_Signal_Mapping = new HashSet<ModuleEntityHwVariant_Signal_Mapping_viewmodel>();
this.ModuleEntityHwVariantSignal_Attribute = new HashSet<ModuleEntityHwVariantSignal_Attribute_viewmodel>();
this.ModuleEntityHwVariantSignal_Plausibility_Mapping = new HashSet<ModuleEntityHwVariantSignal_Plausibility_Mapping_viewmodel>();
this.ModuleProperty_Attribute = new HashSet<ModuleProperty_Attribute_viewmodel>();
this.Plausibilities = new HashSet<Plausibility_viewmodel>();
this.Plausibility_Attribute = new HashSet<Plausibility_Attribute_viewmodel>();
this.Preferences = new HashSet<Preferences_viewmodel>();
this.Signal_Attribute = new HashSet<Signal_Attribute_viewmodel>();
this.Signal_DataType_Mapping = new HashSet<Signal_DataType_Mapping_viewmodel>();
this.Signal_IODPEntity_Mapping = new HashSet<Signal_IODPEntity_Mapping_viewmodel>();
this.Signal_ModuleEntity_Mapping = new HashSet<Signal_ModuleEntity_Mapping_viewmodel>();
this.Signals = new HashSet<Signal_viewmodel>();
this.Types = new HashSet<Type_viewmodel>();
this.ValidValues = new HashSet<ValidValue_viewmodel>();
this.ConfigSets_LockedBy = new HashSet<ConfigSet_viewmodel>();
this.ConfigSet_Attribute_LockedBy = new HashSet<ConfigSet_Attribute_viewmodel>();
this.ConfigSet_DataType_Mapping_LockedBy = new HashSet<ConfigSet_DataType_Mapping_viewmodel>();
this.ConfigSet_IODPEntity_Mapping_LockedBy = new HashSet<ConfigSet_IODPEntity_Mapping_viewmodel>();
this.DataTypes_LockedBy = new HashSet<DataType_viewmodel>();
this.Features_LockedBy = new HashSet<Feature_viewmodel>();
this.GeneralParameters_LockedBy = new HashSet<GeneralParameter_viewmodel>();
this.HwVariantConfigurationMappings_LockedBy = new HashSet<HwVariantConfigurationMapping_viewmodel>();
this.IODPEntities_LockedBy = new HashSet<IODPEntity_viewmodel>();
this.MainFunctions_LockedBy = new HashSet<MainFunction_viewmodel>();
this.MainFunction_Attribute_LockedBy = new HashSet<MainFunction_Attribute_viewmodel>();
this.Modules_LockedBy = new HashSet<Module_viewmodel>();
this.Module_Feature_Mapping_LockedBy = new HashSet<Module_Feature_Mapping_viewmodel>();
this.ModuleEntities_LockedBy = new HashSet<ModuleEntity_viewmodel>();
this.ModuleEntity_Association_LockedBy = new HashSet<ModuleEntity_Association_viewmodel>();
this.ModuleEntity_Attribute_LockedBy = new HashSet<ModuleEntity_Attribute_viewmodel>();
this.ModuleEntity_DataType_Mapping_LockedBy = new HashSet<ModuleEntity_DataType_Mapping_viewmodel>();
this.ModuleEntity_HwVariant_Mapping_LockedBy = new HashSet<ModuleEntity_HwVariant_Mapping_viewmodel>();
this.ModuleEntity_IODPEntity_Mapping_LockedBy = new HashSet<ModuleEntity_IODPEntity_Mapping_viewmodel>();
this.ModuleEntityHwVariant_Attribute_LockedBy = new HashSet<ModuleEntityHwVariant_Attribute_viewmodel>();
this.ModuleEntityHwVariant_ConfigSet_Mapping_LockedBy = new HashSet<ModuleEntityHwVariant_ConfigSet_Mapping_viewmodel>();
this.ModuleEntityHwVariant_MainFunction_Mapping_LockedBy = new HashSet<ModuleEntityHwVariant_MainFunction_Mapping_viewmodel>();
this.ModuleEntityHwVariant_Signal_Mapping_LockedBy = new HashSet<ModuleEntityHwVariant_Signal_Mapping_viewmodel>();
this.ModuleEntityHwVariantSignal_Attribute_LockedBy = new HashSet<ModuleEntityHwVariantSignal_Attribute_viewmodel>();
this.ModuleEntityHwVariantSignal_Plausibility_Mapping_LockedBy = new HashSet<ModuleEntityHwVariantSignal_Plausibility_Mapping_viewmodel>();
this.ModuleProperty_Attribute_LockedBy = new HashSet<ModuleProperty_Attribute_viewmodel>();
this.Plausibilities_LockedBy = new HashSet<Plausibility_viewmodel>();
this.Plausibility_Attribute_LockedBy = new HashSet<Plausibility_Attribute_viewmodel>();
this.Preferences_LockedBy = new HashSet<Preferences_viewmodel>();
this.Signal_Attribute_LockedBy = new HashSet<Signal_Attribute_viewmodel>();
this.Signal_DataType_Mapping_LockedBy = new HashSet<Signal_DataType_Mapping_viewmodel>();
this.Signal_IODPEntity_Mapping_LockedBy = new HashSet<Signal_IODPEntity_Mapping_viewmodel>();
this.Signal_ModuleEntity_Mapping_LockedBy = new HashSet<Signal_ModuleEntity_Mapping_viewmodel>();
this.Signals_LockedBy = new HashSet<Signal_viewmodel>();
this.Types_LockedBy = new HashSet<Type_viewmodel>();
this.ValidValues_LockedBy = new HashSet<ValidValue_viewmodel>();
}
public int Id { get; set; }
public string Name { get; set; }
public string FullName { get; set; }
public bool IsDeleted { get; set; }
public DateTime ModificationDate { get; set; }
public virtual ICollection<ConfigSet_viewmodel> ConfigSets { get; set; }
public virtual ICollection<ConfigSet_Attribute_viewmodel> ConfigSet_Attribute { get; set; }
public virtual ICollection<ConfigSet_DataType_Mapping_viewmodel> ConfigSet_DataType_Mapping { get; set; }
public virtual ICollection<ConfigSet_IODPEntity_Mapping_viewmodel> ConfigSet_IODPEntity_Mapping { get; set; }
public virtual ICollection<DataType_viewmodel> DataTypes { get; set; }
public virtual ICollection<Feature_viewmodel> Features { get; set; }
public virtual ICollection<HwVariant_viewmodel> HwVariants { get; set; }
public virtual ICollection<MainFunction_viewmodel> MainFunctions { get; set; }
public virtual ICollection<MainFunction_Attribute_viewmodel> MainFunction_Attribute { get; set; }
public virtual ICollection<Module_viewmodel> Modules { get; set; }
public virtual ICollection<Module_Feature_Mapping_viewmodel> Module_Feature_Mapping { get; set; }
public virtual ICollection<ModuleEntity_viewmodel> ModuleEntities { get; set; }
public virtual ICollection<ModuleEntity_Association_viewmodel> ModuleEntity_Association { get; set; }
public virtual ICollection<ModuleEntity_Attribute_viewmodel> ModuleEntity_Attribute { get; set; }
public virtual ICollection<ModuleEntity_DataType_Mapping_viewmodel> ModuleEntity_DataType_Mapping { get; set; }
public virtual ICollection<ModuleEntity_HwVariant_Mapping_viewmodel> ModuleEntity_HwVariant_Mapping { get; set; }
public virtual ICollection<ModuleEntityHwVariant_Attribute_viewmodel> ModuleEntityHwVariant_Attribute { get; set; }
public virtual ICollection<ModuleEntityHwVariant_ConfigSet_Mapping_viewmodel> ModuleEntityHwVariant_ConfigSet_Mapping { get; set; }
public virtual ICollection<ModuleEntityHwVariant_MainFunction_Mapping_viewmodel> ModuleEntityHwVariant_MainFunction_Mapping { get; set; }
public virtual ICollection<ModuleEntityHwVariant_Signal_Mapping_viewmodel> ModuleEntityHwVariant_Signal_Mapping { get; set; }
public virtual ICollection<ModuleEntityHwVariantSignal_Attribute_viewmodel> ModuleEntityHwVariantSignal_Attribute { get; set; }
public virtual ICollection<ModuleEntityHwVariantSignal_Plausibility_Mapping_viewmodel> ModuleEntityHwVariantSignal_Plausibility_Mapping { get; set; }
public virtual ICollection<ModuleProperty_Attribute_viewmodel> ModuleProperty_Attribute { get; set; }
public virtual ICollection<Plausibility_viewmodel> Plausibilities { get; set; }
public virtual ICollection<Plausibility_Attribute_viewmodel> Plausibility_Attribute { get; set; }
public virtual ICollection<Signal_Attribute_viewmodel> Signal_Attribute { get; set; }
public virtual ICollection<Signal_DataType_Mapping_viewmodel> Signal_DataType_Mapping { get; set; }
public virtual ICollection<Signal_ModuleEntity_Mapping_viewmodel> Signal_ModuleEntity_Mapping { get; set; }
public virtual ICollection<Signal_viewmodel> Signals { get; set; }
public virtual ICollection<Type_viewmodel> Types { get; set; }
public virtual ICollection<GeneralParameter_viewmodel> GeneralParameters { get; set; }
public virtual ICollection<Preferences_viewmodel> Preferences { get; set; }
public virtual ICollection<IODPEntity_viewmodel> IODPEntities { get; set; }
public virtual ICollection<ModuleEntity_IODPEntity_Mapping_viewmodel> ModuleEntity_IODPEntity_Mapping { get; set; }
public virtual ICollection<Signal_IODPEntity_Mapping_viewmodel> Signal_IODPEntity_Mapping { get; set; }
public virtual ICollection<ValidValue_viewmodel> ValidValues { get; set; }
public virtual ICollection<ConfigSet_viewmodel> ConfigSets_LockedBy { get; set; }
public virtual ICollection<ConfigSet_Attribute_viewmodel> ConfigSet_Attribute_LockedBy { get; set; }
public virtual ICollection<ConfigSet_DataType_Mapping_viewmodel> ConfigSet_DataType_Mapping_LockedBy { get; set; }
public virtual ICollection<ConfigSet_IODPEntity_Mapping_viewmodel> ConfigSet_IODPEntity_Mapping_LockedBy { get; set; }
public virtual ICollection<DataType_viewmodel> DataTypes_LockedBy { get; set; }
public virtual ICollection<Feature_viewmodel> Features_LockedBy { get; set; }
public virtual ICollection<GeneralParameter_viewmodel> GeneralParameters_LockedBy { get; set; }
public virtual ICollection<HwVariant_viewmodel> HwVariants_LockedBy { get; set; }
public virtual ICollection<HwVariantConfigurationMapping_viewmodel> HwVariantConfigurationMappings_LockedBy { get; set; }
public virtual ICollection<IODPEntity_viewmodel> IODPEntities_LockedBy { get; set; }
}
解决方法
您使用的是相当旧的 AutoMapper 版本,但原理与最新版本相似(只是语法可能不同) - 您使用 AutoMapper Projection 而不是 Map
在客户端工作(在您使用它的版本是通过使用 ProjectTo
扩展方法实现的),它基本上生成 Select
运算符将 T
转换为 T1
,但仍然保留它 IQueryable
(结果是 IQueryable<T1>
),因此您可以应用其他 LINQ 运算符,包括 Where
和 Expression<Func<T1,bool>>
。
所以你需要的是添加
using AutoMapper.QueryableExtensions;
然后,假设您的自定义 AutoMapperConfiguration.GetMapperConfiguration()
方法返回 IMapper
实例(由于发布的代码中使用了 Map
方法,因此您最好将其重命名为 GetMapper
来表示它返回的内容),类似这样(当然,您可以删除变量并仅使用 return,为了清楚起见,我添加了它们):
var mapper = AutoMapperConfiguration.GetMapperConfiguration();
var query = _dbContext.Set<T>()
// Here you have IQueryable<T>
.Where(q => !q.IsDeleted)
.ProjectTo<T1>(mapper.ConfigurationProvider) // <-- note the usage of ConfigurationProvider property
// Now you have IQueryable<T1>
.Where(predicate);
var result = query.ToList();
以防万一 GetMapperConfiguration()
返回 MapperConfiguration
实例(不太可能),将其直接传递给 ProjectTo
方法
.ProjectTo<T1>(AutoMapperConfiguration.GetMapperConfiguration())
关于更新。现在,当原始编译问题解决后,您遇到了 Circular reference causing stack overflow with Automapper 问题。这意味着您的视图模型类包含循环引用,例如 ICollction<Child> Parent.Childen
和 Parent Child.Parent
属性。较新版本的 AutoMapper 对此问题有不同的控制,在您的版本中,您应该删除从子级到父级的此类反向引用,或在地图中 Ignore
它们,或在创建地图时使用 MaxDepth(1)
选项,例如
CreateMap<Parent,ParentViewModel>().MaxDepth(1);
CreateMap<Child,ChildViewModel>().MaxDepth(1);