AutoMapper ProjectTo throws NullReferenceException on combined queries with EFCore - entity-framework-core

I am in the process of consolidating queries to minimize the number of SQL round trips.
For results with Skip Take there seems to be a problem with the ProjectTo method of the AutoMapper.
Works:
var totalUsers = await _dbContext.UserAccounts.CountAsync(cancellationToken);
var users = await _dbContext.UserAccounts
.Skip(skip).Take(take)
.ProjectTo<UserProjection>(_mapper.ConfigurationProvider)
.ToListAsync(cancellationToken);
Throws a NullReferenceException:
// query
var query = from _ in _dbContext.UserAccounts
select new
{
TotalUserCount = _dbContext.UserAccounts.Count(),
Users = _dbContext.UserAccounts
.Skip(skip).Take(take)
.ProjectTo<UserProjection>(_mapper.ConfigurationProvider)
.ToList()
};
var result = await query.FirstOrDefaultAsync(cancellationToken);
Stack:
NullReferenceException: Object reference not set to an instance of an object.
Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.AddCollectionProjection(ShapedQueryExpression shapedQueryExpression, INavigation navigation, Type elementType)
Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.Visit(Expression expression)
Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.VisitNew(NewExpression newExpression)
System.Linq.Expressions.NewExpression.Accept(ExpressionVisitor visitor)
Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.Visit(Expression expression)
Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.Translate(SelectExpression selectExpression, Expression expression)
Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateSelect(ShapedQueryExpression source, LambdaExpression selector)
Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor(Expression query)
Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery(Expression query, bool async)
Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore(IDatabase database, Expression query, IModel model, bool async)
Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler+<>c__DisplayClass12_0.b__0()
Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore(object cacheKey, Func> compiler)
Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery(object cacheKey, Func> compiler)
Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync(Expression query, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync(Expression expression, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync(MethodInfo operatorMethodInfo, IQueryable source, Expression expression, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync(MethodInfo operatorMethodInfo, IQueryable source, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.FirstOrDefaultAsync(IQueryable source, CancellationToken cancellationToken)
The problem occurs especially in combination with Skip and Take.
Any idea what could be the reason for this? Or is this a bug of AutoMapper?
The code example is taken from an ASP.NET Core 3.1 application with current AutoMapper version

Related

Linq to SQL cannot translate because RowNumberExpression isn't supported

I have been trying to run a query where I want to group all the records by a specific property, then select the first from each list. I've been through a few iterations of this but it's safe to say the EF Core 3.0 onwards makes this a very hard activity to achieve. I think I might be hitting up against limitations of the JET provider however as I get errors about RowNumberExpression not being able to be translated. The database in question is a very old Access 97 database, which I know is very old and shouldn't be using but I don't have a choice here.
Anyway the query I have ended up with for the time being is as follows:
var sons = await _snContext.TDespatch
.Select(x => x.OrderNumber)
.Distinct()
.SelectMany(x => _snContext.TDespatch.Where(d => x == d.OrderNumber).Take(1))
.ToArrayAsync();
I have been through a few other iterations using GroupBy and then taking the first from each group but it yields similar results.
The error I get is as follows:
Microsoft.EntityFrameworkCore.Query: Error: An exception occurred while iterating over the results of a query for context type 'SNDBConnector.Contexts.SNDBContext'.
System.InvalidOperationException: The LINQ expression '[Microsoft.EntityFrameworkCore.Query.SqlExpressions.RowNumberExpression]' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
at EntityFrameworkCore.Jet.Query.Sql.Internal.JetQuerySqlGenerator.VisitRowNumber(RowNumberExpression rowNumberExpression)
at Microsoft.EntityFrameworkCore.Query.SqlExpressionVisitor.VisitExtension(Expression extensionExpression)
at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.QuerySqlGenerator.VisitProjection(ProjectionExpression projectionExpression)
at Microsoft.EntityFrameworkCore.Query.SqlExpressionVisitor.VisitExtension(Expression extensionExpression)
at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at EntityFrameworkCore.Jet.Query.Sql.Internal.JetQuerySqlGenerator.<VisitSelect>b__7_0(ProjectionExpression e)
at EntityFrameworkCore.Jet.Query.Sql.Internal.JetQuerySqlGenerator.GenerateList[T](IReadOnlyList`1 items, Action`1 generationAction, Action`1 joinAction)
at EntityFrameworkCore.Jet.Query.Sql.Internal.JetQuerySqlGenerator.VisitSelect(SelectExpression selectExpression)
at Microsoft.EntityFrameworkCore.Query.SqlExpressionVisitor.VisitExtension(Expression extensionExpression)
at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at EntityFrameworkCore.Jet.Query.Sql.Internal.JetQuerySqlGenerator.VisitSelect(SelectExpression selectExpression)
at Microsoft.EntityFrameworkCore.Query.SqlExpressionVisitor.VisitExtension(Expression extensionExpression)
at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.QuerySqlGenerator.VisitInnerJoin(InnerJoinExpression innerJoinExpression)
at Microsoft.EntityFrameworkCore.Query.SqlExpressionVisitor.VisitExtension(Expression extensionExpression)
at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at EntityFrameworkCore.Jet.Query.Sql.Internal.JetQuerySqlGenerator.VisitSelect(SelectExpression selectExpression)
at Microsoft.EntityFrameworkCore.Query.QuerySqlGenerator.GetCommand(SelectExpression selectExpression)
at Microsoft.EntityFrameworkCore.Query.Internal.RelationalCommandCache.GetRelationalCommand(IReadOnlyDictionary`2 parameters)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
at EntityFrameworkCore.Jet.Storage.Internal.JetExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
System.InvalidOperationException: The LINQ expression '[Microsoft.EntityFrameworkCore.Query.SqlExpressions.RowNumberExpression]' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
at EntityFrameworkCore.Jet.Query.Sql.Internal.JetQuerySqlGenerator.VisitRowNumber(RowNumberExpression rowNumberExpression)
at Microsoft.EntityFrameworkCore.Query.SqlExpressionVisitor.VisitExtension(Expression extensionExpression)
at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.QuerySqlGenerator.VisitProjection(ProjectionExpression projectionExpression)
at Microsoft.EntityFrameworkCore.Query.SqlExpressionVisitor.VisitExtension(Expression extensionExpression)
at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at EntityFrameworkCore.Jet.Query.Sql.Internal.JetQuerySqlGenerator.<VisitSelect>b__7_0(ProjectionExpression e)
at EntityFrameworkCore.Jet.Query.Sql.Internal.JetQuerySqlGenerator.GenerateList[T](IReadOnlyList`1 items, Action`1 generationAction, Action`1 joinAction)
at EntityFrameworkCore.Jet.Query.Sql.Internal.JetQuerySqlGenerator.VisitSelect(SelectExpression selectExpression)
at Microsoft.EntityFrameworkCore.Query.SqlExpressionVisitor.VisitExtension(Expression extensionExpression)
at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at EntityFrameworkCore.Jet.Query.Sql.Internal.JetQuerySqlGenerator.VisitSelect(SelectExpression selectExpression)
at Microsoft.EntityFrameworkCore.Query.SqlExpressionVisitor.VisitExtension(Expression extensionExpression)
at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.QuerySqlGenerator.VisitInnerJoin(InnerJoinExpression innerJoinExpression)
at Microsoft.EntityFrameworkCore.Query.SqlExpressionVisitor.VisitExtension(Expression extensionExpression)
at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at EntityFrameworkCore.Jet.Query.Sql.Internal.JetQuerySqlGenerator.VisitSelect(SelectExpression selectExpression)
at Microsoft.EntityFrameworkCore.Query.QuerySqlGenerator.GetCommand(SelectExpression selectExpression)
at Microsoft.EntityFrameworkCore.Query.Internal.RelationalCommandCache.GetRelationalCommand(IReadOnlyDictionary`2 parameters)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
at EntityFrameworkCore.Jet.Storage.Internal.JetExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll
I have seen other solutions to this problem on stack overflow but none of them seem to help at all. It seems like a simple thing to me but it also seems like it's an impossible task, but maybe I am being deluded when I think it's a simple task.
Any help with this would be much appreciated.
This is a known limitation of EF core 6.
Unless your database has a relationship that allows an OrderNumber->FirstDespatch navigation, you need to make subqueries. Also you should add an order by clause to make the result consistent.
private async IAsyncEnumerable<TDespatch> GetFirstByOrderNumberAsync()
{
foreach(var number in await _snContext.TDespatch
.Select(x => x.OrderNumber)
.Distinct()
.ToListAsync()
.ConfigureAwait(false))
{
yield return await _snContext.TDespatch
.AsNoTracking()
.Where(despatch => number == despatch.OrderNumber)
// insert an order by clause here
.FirstAsync()
.ConfigureAwait(false);
}
}
So I worked with a DBA on my team and we managed to come up with a SQL version of what I needed, which worked really well when injecting directly into EF as raw SQL. The next step I took was to download a program called Linqer. I created a SQL Server version of the Access database so that Linqer could connect to it and test, then I put the SQL version of the query into Linqer and it automatically converted the SQL to Linq. With a few tweaks it has given me exactly what I wanted. The Linq if anyone is interested is here:
await (from Despatch in context.Despatch
where Despatch.DespatchDate >= startDate && Despatch.DespatchDate <= endDate
group Despatch by new
{
Despatch.OrderNumber
} into g
select new Despatch
{
OrderNumber = g.Key.OrderNumber,
DespatchDate = g.Min(p => p.DespatchDate),
RepCalNumber = g.Min(p => p.RepCalNumber),
SerialNumber = g.Min(p => p.SerialNumber)
})
.OrderByDescending(x => x.DespatchDate)
.ToArrayAsync();
This runs very quickly on that huge Access database, especially with the date constraints that I've added in. I think the key here is using the Min function, which Access is able to understand. Thanks to those who attempted to help me!

An exception occurred while iterating over the results of a query for context type 'Volo.Saas.EntityFrameworkCore.SaasDbContext

ABP Framework version: v4.4.3
UI type: MVC
DB provider: EF Core
Tiered (MVC) or Identity Server Separated (Angular): yes
Exception message and stack trace:
2021-11-03 21:21:43.774 -05:00 [ERR] An exception occurred while iterating over the results of a query for context type 'Volo.Saas.EntityFrameworkCore.SaasDbContext'.
System.Threading.Tasks.TaskCanceledException: A task was canceled.
at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.BufferedDataRecord.InitializeAsync(DbDataReader reader, IReadOnlyList1 columns, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.InitializeAsync(IReadOnlyList1 columns, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.InitializeAsync(IReadOnlyList1 columns, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.SplitQueryingEnumerable1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func4 operation, Func4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.SplitQueryingEnumerable1.AsyncEnumerator.MoveNextAsync() System.Threading.Tasks.TaskCanceledException: A task was canceled. at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.BufferedDataRecord.InitializeAsync(DbDataReader reader, IReadOnlyList1 columns, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.InitializeAsync(IReadOnlyList1 columns, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.InitializeAsync(IReadOnlyList1 columns, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.SplitQueryingEnumerable1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func4 operation, Func4 verifySucceeded, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.SplitQueryingEnumerable1.AsyncEnumerator.MoveNextAsync()
2021-11-03 21:21:43.838 -05:00 [ERR] A task was canceled.
System.Threading.Tasks.TaskCanceledException: A task was canceled.
at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.BufferedDataRecord.InitializeAsync(DbDataReader reader, IReadOnlyList1 columns, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.InitializeAsync(IReadOnlyList1 columns, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.InitializeAsync(IReadOnlyList1 columns, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.SplitQueryingEnumerable1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func4 operation, Func4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.SplitQueryingEnumerable1.AsyncEnumerator.MoveNextAsync() at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable1 asyncEnumerable, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable1 asyncEnumerable, CancellationToken cancellationToken) at Volo.Abp.Domain.Repositories.EntityFrameworkCore.EfCoreRepository3.FindAsync(TKey id, Boolean includeDetails, CancellationToken cancellationToken)
at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo)
at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue1.ProceedAsync() at Volo.Abp.Uow.UnitOfWorkInterceptor.InterceptAsync(IAbpMethodInvocation invocation) at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func3 proceed) at Volo.Saas.Tenants.TenantStore.GetCacheItemAsync(Nullable1 id, String name)
at Volo.Saas.Tenants.TenantStore.FindAsync(Guid id)
at ClientsLink.Data.ClientsLinkTenantDatabaseMigrationHandler.MigrateAndSeedForTenantAsync(Guid tenantId, String adminEmail, String adminPassword) in D:\Century\Internal\Clients.Link\Clients.Link\src\ClientsLink.Domain\Data\ClientsLinkTenantDatabaseMigrationHandler.cs:line 98
This is occurring on the highlighted line of code below:
private async Task MigrateAndSeedForTenantAsync(
Guid tenantId,
string adminEmail,
string adminPassword)
{
try
{
using (_currentTenant.Change(tenantId))
{
// Create database tables if needed
using (var uow = _unitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
{
var tenantConfiguration = await _tenantStore.FindAsync(tenantId); // EXCEPTION THROWN HERE
if (tenantConfiguration?.ConnectionStrings != null &&
!tenantConfiguration.ConnectionStrings.Default.IsNullOrWhiteSpace())
{
foreach (var migrator in _dbSchemaMigrators)
{
_logger.LogDebug($"Running migration {migrator.ToString()}");
await migrator.MigrateAsync();
}
}
await uow.CompleteAsync();
}
Steps to reproduce the issue:
Use ABP Commercial Suite v 4.4.3 to generate a Sample Tiered MVC application
Add a couple of entities - one global and one multitenant with a navigation property to the global one
Create a Sample database
Run the DbMigrator - host database will be migrated successfully
Run the web application, login as admin, create a new Test tenant - after about a minute, the UI reports an Internal Error and the exception above is logged
It seems fairly obvious after working this for two weeks, there is something preventing the execution of the _tenantStore.FindAsync(tenantId) call.
What possible things could cause that call to timeout or be canceled?
Please think about your response and be as verbose as possible.
I have swapped out every component. I have built and rebuilt on two separate machines. I have rewritten my data seed contributors. I have swapped out System.Data.SqlClient for Microsoft.Data.SqlClient and set the Command Timeout connection string argument two 3600. Nothing I have tried in two weeks is resolving this issue.
Finally found an answer in this post: https://support.abp.io/QA/Questions/2084/An-exception-occurred-while-iterating-over-the-results-of-a-query-for-context-type-%27VoloSaasEntityFrameworkCoreSaasDbContexspendinng
As you see from the screenshot, the solution was setting the requiresNew arguments to false. This is necessary because there was already a transaction in process started by the [UnitOfWork] attribute on a calling method.

"System.NullReferenceException" using EF.Property while querying a database EntityFrameworkCore

community. I'm trying to query a database using spatial data and PostgreSQL. I use NetTopologySuite. So far so good. Now, my model does not use the NetTopologySuitePoint class which has a method called DistanceTo which is translated in the database. So, in ModelCreating, I map this property and add a conversion to-from Point. Everything is fine here, I can query the database using SQL commands, but, now, I need to do it with EntityFrameworkCore, using LINQ.
To convert I use:
builder.Entity<Address>(x =>
{
x.ToTable("Addresses");
x.Property(b => b.Coordinates).HasConversion(
v => new Point(v.Latitude, v.Longitude),
v => new Protos.Coordinates { Latitude = (float)v.X, Longitude = (float)v.Y });
});
When I want to query:
await mRepository.AsQueryable<Business>()
.Include(x => x.TradeBranchs).ThenInclude(x => x.Address)
.Where(x => x.Category == category)
.Where(x => x.TradeBranchs.Any(b => EF.Property<Point>(b.Address, nameof(Address.Coordinates)).Distance(customerPoint) <= (distance / 1.609)))
.PaginateAsync(currentPage, pageSize);
Here I use EF.Property<NetTopologySuite.Geometries.Point>(b.Address, nameof(Address.Coordinates)) because how I said previously, i couldn't use Distance with my own Coordinates class.
but I don't know why the following exception is thrown:
System.NullReferenceException: Object reference not set to an instance of an object. at Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionTranslators.Internal.NpgsqlGeometryMethodTranslator.TranslateGeometryMethod(SqlExpression instance, MethodInfo method, IReadOnlyList`1 arguments
at Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionTranslators.Internal.NpgsqlGeometryMethodTranslator.Translate(SqlExpression instance, MethodInfo method, IReadOnlyList`1 arguments, IDiagnosticsLogger`1 logger
at Microsoft.EntityFrameworkCore.Query.RelationalMethodCallTranslatorProvider.<>c__DisplayClass4_0.<Translate>b__3(IMethodCallTranslator t
at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext(
at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate
at Microsoft.EntityFrameworkCore.Query.RelationalMethodCallTranslatorProvider.Translate(IModel model, SqlExpression instance, MethodInfo method, IReadOnlyList`1 arguments, IDiagnosticsLogger`1 logger
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression
at Npgsql.EntityFrameworkCore.PostgreSQL.Query.Internal.NpgsqlSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCall
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitBinary(BinaryExpression binaryExpression
at Npgsql.EntityFrameworkCore.PostgreSQL.Query.Internal.NpgsqlSqlTranslatingExpressionVisitor.VisitBinary(BinaryExpression binaryExpression
at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitBinary(BinaryExpression binaryExpression
at Npgsql.EntityFrameworkCore.PostgreSQL.Query.Internal.NpgsqlSqlTranslatingExpressionVisitor.VisitBinary(BinaryExpression binaryExpression
at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.TranslateInternal(Expression expression
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.Translate(Expression expression
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateExpression(Expression expression
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateLambdaExpression(ShapedQueryExpression shapedQueryExpression, LambdaExpression lambdaExpression
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateWhere(ShapedQueryExpression source, LambdaExpression predicate
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateAny(ShapedQueryExpression source, LambdaExpression predicate
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.TranslateSubquery(Expression expression
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression
at Npgsql.EntityFrameworkCore.PostgreSQL.Query.Internal.NpgsqlSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCall
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.TranslateInternal(Expression expression
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.Translate(Expression expression
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateExpression(Expression expression
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateLambdaExpression(ShapedQueryExpression shapedQueryExpression, LambdaExpression lambdaExpression
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateWhere(ShapedQueryExpression source, LambdaExpression predicate
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_0`1.<ExecuteAsync>b__0(
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetAsyncEnumerator(CancellationToken cancellationToken
at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable`1.GetAsyncEnumerator(
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken
at Reddi.Backend.Manager.UserManager.GetBusinessesAsync(Address customerAddress, Double distance, BusinessCategory category, Int32 pageSize, Int32 currentPage) in /workspace/src/reddi/Backend/master/Reddi.Backend.Master/Manager/UserManager.cs:line 814
It seems like somewhere its null, but where?

Unable to determine the serialization information for "Expression"

I receive the error message Unable to determine the serialization information for a => a.CurrentProcessingStep when I try to Update my document.
I'm using the Mongo .NET Driver version 2.8 and .NET Framework 4.7.2.
I'm building the filter and update statements using the builder helpers from the library.
Here is the relevant section of my document class:
[BsonIgnoreExtraElements]
[DebuggerDisplay("{Term}, Rule Status = {_ruleStatus}, Current Step = {_currentProcessingStep}")]
public class SearchTermInfo : AMongoConnectedObject
{
[BsonElement("CurrentProcessingStep")]
private ProcessingStep _currentProcessingStep;
[BsonElement("RuleStatus")]
private RuleStatus _ruleStatus;
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
public DateTime AddDateTime { get; set; }
[BsonIgnore]
[JsonIgnore]
public ProcessingStep CurrentProcessingStep
{
get => _currentProcessingStep;
set => AddHistoryRecord(value);
}
[BsonIgnore]
[JsonIgnore]
public RuleStatus RuleStatus
{
get => _ruleStatus;
set => AddHistoryRecord(value);
}
I'm building the filter with this code:
FilterDefinition<SearchTermInfo> filter = Builders<SearchTermInfo>.Filter.And(
Builders<SearchTermInfo>.Filter.Eq(a => a.Id, recordId),
Builders<SearchTermInfo>.Filter.Or(
Builders<SearchTermInfo>.Filter.Eq(a => a.CurrentProcessingStep, currentStep),
Builders<SearchTermInfo>.Filter.Exists("CurrentProcessingStep", false)
)
);
Then I execute the update:
UpdateResult results =
searchTermInfoCollection.MongoCollection.UpdateOne(filter, update, null, cancellationToken);
and I get this error:
? ex
{"Unable to determine the serialization information for a => a.CurrentProcessingStep."}
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233079
HelpLink: null
InnerException: null
Message: "Unable to determine the serialization information for a => a.CurrentProcessingStep."
Source: "MongoDB.Driver"
StackTrace: " at MongoDB.Driver.ExpressionFieldDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, Boolean allowScalarValueForArrayField)
at MongoDB.Driver.SimpleFilterDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)\r\n
at MongoDB.Driver.OrFilterDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)\r\n
at MongoDB.Driver.AndFilterDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)\r\n
at MongoDB.Driver.MongoCollectionImpl`1.ConvertWriteModelToWriteRequest(WriteModel`1 model, Int32 index)\r\n
at System.Linq.Enumerable.<SelectIterator>d__5`2.MoveNext()\r\n
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n
at MongoDB.Driver.Core.Operations.BulkMixedWriteOperation..ctor(CollectionNamespace collectionNamespace, IEnumerable`1 requests, MessageEncoderSettings messageEncoderSettings)\r\n
at MongoDB.Driver.MongoCollectionImpl`1.CreateBulkWriteOperation(IEnumerable`1 requests, BulkWriteOptions options)\r\n
at MongoDB.Driver.MongoCollectionImpl`1.BulkWrite(IClientSessionHandle session, IEnumerable`1 requests, BulkWriteOptions options, CancellationToken cancellationToken)\r\n
at MongoDB.Driver.MongoCollectionImpl`1.<>c__DisplayClass23_0.<BulkWrite>b__0(IClientSessionHandle session)\r\n
at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSession[TResult](Func`2 func, CancellationToken cancellationToken)\r\n
at MongoDB.Driver.MongoCollectionImpl`1.BulkWrite(IEnumerable`1 requests, BulkWriteOptions options, CancellationToken cancellationToken)\r\n
at MongoDB.Driver.MongoCollectionBase`1.UpdateOne(FilterDefinition`1 filter, UpdateDefinition`1 update, UpdateOptions options, Func`3 bulkWrite)\r\n
at MongoDB.Driver.MongoCollectionBase`1.UpdateOne(FilterDefinition`1 filter, UpdateDefinition`1 update, UpdateOptions options, CancellationToken cancellationToken)\r\n
at SaFileIngestion.FileProcessorEngine.LockRecord(Int64 recordId, ProcessingStep currentStep, CancellationToken cancellationToken)
in D:\\[File Path]\\SaFileIngestion\\FileProcessorEngine.cs:line 195"
TargetSite: {MongoDB.Driver.RenderedFieldDefinition`1[TField] Render(MongoDB.Bson.Serialization.IBsonSerializer`1[TDocument], MongoDB.Bson.Serialization.IBsonSerializerRegistry, Boolean)}
I've also tried to build the filter with this code:
Builders<SearchTermInfo>.Filter.Eq("CurrentProcessingStep", currentStep),
It was my impression that I should be able to build an expression using the property accessor, even though it is flagged as BsonIgnore, since the private field has been attributed with BsonElementAttribute with the same name as the ignored property accessor.
Any guidance provided is appreciated.
Thanks
I eventually resolved this. I don't remember the exact solution as it was over a year ago. I do remember there was something wrong with the way the query was being constructed.

ServiceFabric / IFabricSecretStoreClient / COM Exception

I'm not finding much on this, and after spending most of the day...I'm looking for help..
The exception I get is this:
Unable to cast COM object of type 'System.__ComObject' to interface
type 'IFabricSecretStoreClient'. This operation failed because the
QueryInterface call on the COM component for the interface with IID
'{38C4C723-3815-49D8-BDF2-68BFB536B8C9}' failed due to the following
error: No such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE)).
I'm able to instantiate the proxy to the Stateful Service with this code:
var servicekey = new ServicePartitionKey(0);
var queryserviceUri = new Uri("fabric:/XXXX/xxxxxxxxxx");
var proxyHandle = ServiceProxy.Create<IAnInterfaceName>(queryserviceUri, servicekey, TargetReplicaSelector.PrimaryReplica);
Then I use the proxy:
var result = await proxyHandle.OperationOnServiceContract(dataPackage);
At this point I get the error...
Stacktrace...
at System.Fabric.FabricClient.CreateNativeClient(IEnumerable1
connectionStringsLocal) at
System.Fabric.Interop.Utility.<>c__DisplayClass27_0.<WrapNativeSyncInvoke>b__0()
at System.Fabric.Interop.Utility.WrapNativeSyncInvoke[TResult](Func1
func, String functionTag, String functionArgs) at
System.Fabric.Interop.Utility.WrapNativeSyncInvoke(Action action,
String functionTag, String functionArgs) at
System.Fabric.Interop.Utility.RunInMTA(Action action) at
System.Fabric.FabricClient.InitializeFabricClient(SecurityCredentials
credentialArg, FabricClientSettings newSettings, String[]
hostEndpointsArg) at
Microsoft.ServiceFabric.Services.Client.ServicePartitionResolver.<>c.b__21_0()
at
Microsoft.ServiceFabric.Services.Client.ServicePartitionResolver.GetClient()
at
Microsoft.ServiceFabric.Services.Client.ServicePartitionResolver.ResolveHelperAsync(Func5
resolveFunc, ResolvedServicePartition previousRsp, TimeSpan
resolveTimeout, TimeSpan maxRetryInterval, CancellationToken
cancellationToken, Uri serviceUri) at
Microsoft.ServiceFabric.Services.Communication.Client.CommunicationClientFactoryBase1.GetClientAsync(Uri
serviceUri, ServicePartitionKey partitionKey, TargetReplicaSelector
targetReplicaSelector, String listenerName, OperationRetrySettings
retrySettings, CancellationToken cancellationToken) at
Microsoft.ServiceFabric.Services.Remoting.V2.FabricTransport.Client.FabricTransportServiceRemotingClientFactory.GetClientAsync(Uri
serviceUri, ServicePartitionKey partitionKey, TargetReplicaSelector
targetReplicaSelector, String listenerName, OperationRetrySettings
retrySettings, CancellationToken cancellationToken) at
Microsoft.ServiceFabric.Services.Communication.Client.ServicePartitionClient1.GetCommunicationClientAsync(CancellationToken
cancellationToken) at
Microsoft.ServiceFabric.Services.Communication.Client.ServicePartitionClient1.InvokeWithRetryAsync[TResult](Func2
func, CancellationToken cancellationToken, Type[]
doNotRetryExceptionTypes) at
Microsoft.ServiceFabric.Services.Remoting.V2.Client.ServiceRemotingPartitionClient.InvokeAsync(IServiceRemotingRequestMessage
remotingRequestMessage, String methodName, CancellationToken
cancellationToken) at
Microsoft.ServiceFabric.Services.Remoting.Builder.ProxyBase.InvokeAsyncV2(Int32
interfaceId, Int32 methodId, String methodName,
IServiceRemotingRequestMessageBody requestMsgBodyValue,
CancellationToken cancellationToken) at
Microsoft.ServiceFabric.Services.Remoting.Builder.ProxyBase.ContinueWithResultV2[TRetval](Int32
interfaceId, Int32 methodId, Task1 task) at
XXXXWeb.Controllers.XXXController.OperationOnServiceContract(XXXRequest
xxxRequest) in
C:\Users\codputer\Source\Repos\xxxxxx\xxxx.Web\Controllers\XXXController.cs:line
44
oh I'm using this for a listener:
protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
{
return this.CreateServiceRemotingReplicaListeners();
}
As noted on these issues:
#476#Azure/service-fabric-issues
#1374#Azure/service-fabric-issues
#262#Microsoft/service-fabric
These COM issues happens whenever you use newer Nuget packages targeting older version of service fabric runtime, this kind of error occurs.
You should always use the packages targeted to the same version of your cluster runtime, on last case, use package older than the cluster version.