我正在使用AutoMapper extensions for ASP.NET Core,所以我在Startup注册了Automapper,就像在这个官方example中一样。 我想在启动时编译映射而不是让Automapper执行默认的延迟编译,但似乎无法通过此设置找出如何进行急切编译(记录为here)。我尝试了以下内容,但据我所知,它不会导致编译:
services.AddAutoMapper();
Mapper.Configuration.CompileMappings();
我试图解决的问题是,一个特定的映射相当复杂,调用多个格式化函数,并且第一次命中总是很慢。我希望在应用程序启动时而不是第一次执行地图时产生费用。
编辑:经过进一步调查后,性能影响似乎来自查询的EF核心编译,这导致我尝试预编译查询,如下所示:
var query = EF.CompileQuery((MyProjectContext db, int id) =>
db.Product.ProjectTo<Models.Product>().FirstOrDefault()
);
var product= query(context, 1);
然而,当我尝试执行时,似乎ProjectTo无法与EF.CompileQuery一起使用,因为我收到此错误:
Test method MyProject.Test.MappingTest.ProductGetPreCompile threw exception:
System.NotSupportedException: Could not parse expression 'value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[MyProject.Data.Entities.Product]).ProjectTo(value(System.Linq.Expressions.Expression`1[System.Func`2[MyProject.Web.Models.Product,System.Object]][]))': This overload of the method 'AutoMapper.QueryableExtensions.Extensions.ProjectTo' is currently not supported.
at Remotion.Linq.Parsing.Structure.MethodCallExpressionParser.GetNodeType(MethodCallExpression expressionToParse)
at Remotion.Linq.Parsing.Structure.MethodCallExpressionParser.Parse(String associatedIdentifier, IExpressionNode source, IEnumerable`1 arguments, MethodCallExpression expressionToParse)
at Remotion.Linq.Parsing.Structure.ExpressionTreeParser.ParseMethodCallExpression(MethodCallExpression methodCallExpression, String associatedIdentifier)
at Remotion.Linq.Parsing.Structure.ExpressionTreeParser.ParseNode(Expression expression, String associatedIdentifier)
at Remotion.Linq.Parsing.Structure.ExpressionTreeParser.ParseMethodCallExpression(MethodCallExpression methodCallExpression, String associatedIdentifier)
at Remotion.Linq.Parsing.Structure.ExpressionTreeParser.ParseTree(Expression expressionTree)
at Remotion.Linq.Parsing.Structure.QueryParser.GetParsedQuery(Expression expressionTreeRoot)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, INodeTypeProvider nodeTypeProvider, IDatabase database, IDiagnosticsLogger`1 logger, Type contextType)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CreateCompiledQuery[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQuery`2.CreateCompiledQuery(IQueryCompiler queryCompiler, Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryBase`2.<>c__DisplayClass6_0.<EnsureExecutor>b__0(TContext c, LambdaExpression q)
at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam1,TParam2,TValue](TValue& target, TParam1 param1, TParam2 param2, Func`3 valueFactory)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryBase`2.EnsureExecutor(TContext context)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryBase`2.ExecuteCore(TContext context, CancellationToken cancellationToken, Object[] parameters)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryBase`2.ExecuteCore(TContext context, Object[] parameters)
at MyProject.Test.MappingTest.ProductGetPreCompile() in C:\projects\MyCompany\MyCustomer\MyProject\MyProject.Test\MappingTest.cs:line 47