question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[bug]Issue with linq2db in dotnet 7 ef core model with nullable fields

See original GitHub issue

hi i think this is a possible bug, can you check this query:

pageRepository.Query().Where(x => x.IsActive == true && x.SiteId == accountService.GetSiteId() && x.RecordStatus == RecordStatus.Published)
                   .Select(x => new SlugCache
                   {
                       SlugUrl = x.PageSlugUrl,
                       LastRequested = x.LastRequested,
                       DateModified = x.DateModified,
                       PageTypeName = pageTypesRepository.Query().Where(t => t.Id == x.PageTypeId).Select(t => t.PageTypeName).FirstOrDefault(),
                       IsHomePage = x.IsHomePage
                   }).ToList()

but if i add :

pageRepository.Query().Where(x => x.IsActive == true && x.SiteId == accountService.GetSiteId() && x.RecordStatus == RecordStatus.Published)
                   .Select(x => new SlugCache
                   {
                       SlugUrl = x.PageSlugUrl,
                       LastRequested = x.LastRequested,
                       DateModified = x.DateModified,
                       PageTypeName = pageTypesRepository.Query().Where(t => t.Id == x.PageTypeId).Select(t => t.PageTypeName).ToLinqToDB().FirstOrDefault(),
                       IsHomePage = x.IsHomePage
                   }).ToLinqToDB().ToList()

i am getting following error:

Expression 'x.IsActive' is not a Field.
its defined in model as public new bool? IsActive { get; set; } = true;

full stacktrace:

LinqToDB.Linq.LinqException: Expression 'x.IsActive' is not a Field.
   at LinqToDB.Linq.Builder.TableBuilder.TableContext.ConvertToSql(Expression expression, Int32 level, ConvertFlags flags)
   at LinqToDB.Linq.Builder.TableBuilder.TableContext.ConvertToIndex(Expression expression, Int32 level, ConvertFlags flags)
   at LinqToDB.Linq.Builder.SubQueryContext.ConvertToSql(Expression expression, Int32 level, ConvertFlags flags)
   at LinqToDB.Linq.Builder.ExpressionContext.ConvertToSql(Expression expression, Int32 level, ConvertFlags flags)
   at LinqToDB.Linq.Builder.ExpressionBuilder.ConvertToSql(IBuildContext context, Expression expression, Boolean unwrap, ColumnDescriptor columnDescriptor, Boolean isPureExpression)
   at LinqToDB.Linq.Builder.ExpressionBuilder.ConvertCompare(IBuildContext context, ExpressionType nodeType, Expression left, Expression right)
   at LinqToDB.Linq.Builder.ExpressionBuilder.ConvertPredicate(IBuildContext context, Expression expression)
   at LinqToDB.Linq.Builder.ExpressionBuilder.BuildSearchCondition(IBuildContext context, Expression expression, List`1 conditions)
   at LinqToDB.Linq.Builder.ExpressionBuilder.BuildSearchCondition(IBuildContext context, Expression expression, List`1 conditions)
   at LinqToDB.Linq.Builder.ExpressionBuilder.BuildSearchCondition(IBuildContext context, Expression expression, List`1 conditions)
   at LinqToDB.Linq.Builder.ExpressionBuilder.BuildWhere(IBuildContext parent, IBuildContext sequence, LambdaExpression condition, Boolean checkForSubQuery, Boolean enforceHaving)
   at LinqToDB.Linq.Builder.WhereBuilder.BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
   at LinqToDB.Linq.Builder.MethodCallBuilder.BuildSequence(ExpressionBuilder builder, BuildInfo buildInfo)
   at LinqToDB.Linq.Builder.ExpressionBuilder.BuildSequence(BuildInfo buildInfo)
   at LinqToDB.Linq.Builder.SelectBuilder.BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
   at LinqToDB.Linq.Builder.MethodCallBuilder.BuildSequence(ExpressionBuilder builder, BuildInfo buildInfo)
   at LinqToDB.Linq.Builder.ExpressionBuilder.BuildSequence(BuildInfo buildInfo)
   at LinqToDB.Linq.Builder.ExpressionBuilder.Build[T]()
   at LinqToDB.Linq.Query`1.CreateQuery(ExpressionTreeOptimizationContext optimizationContext, ParametersContext parametersContext, IDataContext dataContext, Expression expr)
   at LinqToDB.Linq.Query`1.GetQuery(IDataContext dataContext, Expression& expr, Boolean& dependsOnParameters)
   at LinqToDB.Linq.ExpressionQuery`1.GetQuery(Expression& expression, Boolean cache, Boolean& dependsOnParameters)
   at LinqToDB.Linq.ExpressionQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at LinqToDB.EntityFrameworkCore.Internal.LinqToDBForEFQueryProvider`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

please check linq2db.efcore version 7.5.0

Issue Analytics

  • State:open
  • Created a month ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Aug 15, 2023

If you add .ToLinqToDB() in projection? It has no sense.

0reactions
sdanylivcommented, Aug 15, 2023

Post your model classes. Base class and this one withIsActive. Will check what happened.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with nullable reference types - EF Core
This page introduces EF Core's support for nullable reference types, and describes best practices for working with them.
Read more >
How to debug and fix 'Nullable object must have a value ...
There can be two answers. Firstly, this may be probably because your query is returning NULL and the model is not accepting null....
Read more >
Invalid ThenInclude() Nullable Reference Type Warning
I am trying to use EF Core with the new C# 8.0 nullable reference types, and I am trying to find the right...
Read more >
EF Core throws an error when attempting to insert data with ...
EF Core throws an error when attempting to insert data with a nullable foreign key. I have the following association :
Read more >
How do you manage non-loaded navigation properties in ...
You can make navigation properies non-nullable for Linq to remove bangs, and make method return nullable type if it can be null in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found