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.

How use with EfCore Shadow Property

See original GitHub issue

Description

When i use EfCore Shadow Property, The following error occurs: LinqToDB.Linq.LinqException:“‘Property(p, “IsDeleted”)’ cannot be converted to SQL.”

Stack Trace

LinqToDB.Linq.LinqException
  HResult=0x80131500
  Message='Property(p, "IsDeleted")' cannot be converted to SQL.
  Source=linq2db
  StackTrace:
   at LinqToDB.Linq.Builder.ExpressionBuilder.ConvertToSql(IBuildContext context, Expression expression, Boolean unwrap, ColumnDescriptor columnDescriptor, Boolean isPureExpression)
   at LinqToDB.Linq.Builder.ExpressionBuilder.ConvertPredicate(IBuildContext context, Expression expression)
   at LinqToDB.Linq.Builder.ExpressionBuilder.ConvertToSql(IBuildContext context, Expression expression, Boolean unwrap, ColumnDescriptor columnDescriptor, Boolean isPureExpression)
   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.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.TableBuilder.ApplyQueryFilters(ExpressionBuilder builder, BuildInfo buildInfo, MemberInfo memberInfo, TableContext tableContext)
   at LinqToDB.Linq.Builder.TableBuilder.BuildSequence(ExpressionBuilder builder, BuildInfo buildInfo)
   at LinqToDB.Linq.Builder.ExpressionBuilder.BuildSequence(BuildInfo buildInfo)
   at LinqToDB.Linq.Builder.OrderByBuilder.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.TakeSkipBuilder.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.TakeSkipBuilder.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(IDataContext dataContext, Expression expr)
   at LinqToDB.Linq.Query`1.GetQuery(IDataContext dataContext, Expression& expr)
   at LinqToDB.Linq.ExpressionQuery`1.GetQuery(Expression& expression, Boolean cache)
   at LinqToDB.Linq.ExpressionQuery`1.<GetForEachAsync>d__19.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
   at LinqToDB.AsyncExtensions.<ToListAsync>d__8`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at ConsoleApp3.Program.<Main>d__0.MoveNext() in H:\Code\linq2db.EntityFrameworkCore\ConsoleApp3\Program.cs:line 30

Test Code

using System;
using System.Linq;
using System.Threading.Tasks;
using LinqToDB.EntityFrameworkCore.BaseTests;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using LinqToDB;
using LinqToDB.EntityFrameworkCore;

namespace ConsoleApp3
{
	static class Program
	{
		static async Task Main(string[] args)
		{
			var configuration = new ConfigurationBuilder()
				.AddJsonFile("appsettings.json", optional: false)
				.Build();
			var dbContextOptions = new DbContextOptionsBuilder<AbpDbContext>()
				.UseSqlServer(configuration.GetConnectionString("Default"))
				.UseLoggerFactory(TestUtils.LoggerFactory)
				.Options;
			using var dbContext = new AbpDbContext(dbContextOptions);
			var users = await dbContext.AbpUsers.OrderBy(p=>p.Id).Skip(10).Take(100).ToListAsyncEF();
			Console.WriteLine(users.Count);

			LinqToDBForEFTools.Initialize();
			using var db = dbContext.CreateLinqToDbContext();
			var queryable = db.GetTable<AbpUser>().OrderBy(p=>p.Id).Skip(10).Take(100);
			var linqUsers = await queryable.ToListAsyncLinqToDB();
			Console.WriteLine(linqUsers.Count);
			var tempUsers = await db.CreateTempTableAsync(queryable, "#TempAbpUsers");
			var result = await tempUsers.ToListAsyncLinqToDB();
			Console.WriteLine(result.Count);
			Console.WriteLine("Hello World!");
		}
	}

	public class AbpDbContext : DbContext
	{
		public DbSet<AbpUser> AbpUsers { set; get; }

		public AbpDbContext(DbContextOptions<AbpDbContext> options) : base(options)
		{

		}

		protected override void OnModelCreating(ModelBuilder modelBuilder)
		{
			modelBuilder.Entity<AbpUser>(p =>
			{
				p.HasKey(p => p.Id);
				p.Property<bool>("IsDeleted").IsRequired();
				p.HasQueryFilter(p => !EF.Property<bool>(p, "IsDeleted"));
			});

			base.OnModelCreating(modelBuilder);
		}
	}

	public class AbpUser
	{
		/// <summary>
		/// ID
		/// </summary>
		public Guid Id { set; get; }

		/// <summary>
		/// UserName
		/// </summary>
		public string UserName { set; get; }
	}
}

Enviroment

OS: Windows 10
SDK: NET5
Database: SQL Server 2019

Please tell me What should I do? Thanks

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Aug 13, 2021

Well, i have to rethink that. Anyway you can create any temp table from any class. Just add shadow property as real one.

0reactions
NetChenQiancommented, Aug 13, 2021

You can also give developers the option to ignore or not to ignore shadow properties to accommodate different scenarios

Read more comments on GitHub >

github_iconTop Results From Across the Web

Shadow and Indexer Properties - EF Core
Shadow properties are properties that aren't defined in your .NET entity class but are defined for that entity type in the EF Core...
Read more >
Entity Framework Core Shadow Properties
Shadow properties can be referenced in LINQ queries via the static Property method of the EF utility class: var contacts = context.Contacts.
Read more >
How to Use the Shadow Property in Entity Framework Core
Shadow property is also called as Navigation, Auditable or Tracking property. (See the Docs.) The database table contains the shadow properties ...
Read more >
Explore Shadow Properties in EF Core | by Saeed Ganji
To access and set the values of shadow properties, you could use the change tracker or static method Property of EF class. context.Entry(author).Property(" ......
Read more >
EF Core: How to add the relationship to shadow property?
Shadow properties represent a primitive properties (FK in your case) and cannot be used to create navigation property.
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