linq2db: TEntity cannot convert to SQL

Describe your issue.

Hi, i want to update database object full dynamically but linq2db cannot create sql from TEntitiy

here is my function

public async Task UpdateEntity(Expression<Func<TEntity, bool>> predicate, TEntity entity)
{
  await Context.GetTable<TEntity>().Where(predicate).UpdateAsync(x => entity);
}

Cann someone help me to fix this issue?

Exception message:
Stack trace: LinqToDB.Linq.LinqException : 'value(AlfaTools.Interface.GDI.Data.GDIRepository`1+<>c__DisplayClass12_0[AlfaTools.Interface.GDI.KUNDEN]).entity' cannot be converted to SQL.

Environment details

linq2db version: 3.3.0 Database Server: Windows Server 2012 Database Provider: Firebird 3.0 Operating system: Windows .NET Framework: 5.0

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (8 by maintainers)

Most upvoted comments

Use Expression, we cannot get this information without ExpressionTree

await UpdateCustomer(1202579, x => new KUNDEN() { BEMERK2 = updatevalue });

Well, this one should work, as you have shown previously. Also added dynamic variant

public async Task UpdateEntity(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TEntity>> updateExpr) 
{
	await Context.GetTable<TEntity>().UpdateAsync(predicate, updateExpr);
}

public async Task UpdateEntity(Expression<Func<TEntity, bool>> predicate, TEntity entity) 
{
	var ed = Context.MappingSchema.GetEntityDescriptor(typeof(TEntity));
	var updateColumns =
		ed.Columns.Where(c => !c.SkipOnUpdate && !c.ShouldSkip(entity, ed, SkipModification.Update)).ToList();

	if (updateColumns.Count == 0)
		return;

	var param      = Expression.Parameter(typeof(TEntity), "e");
	var entityExpr = Expression.Constant(entity);
	var bindings = updateColumns.Select(c =>
		Expression.Bind(c.MemberInfo, Expression.MakeMemberAccess(entityExpr, c.MemberInfo)));

	var updateExpr = Expression.Lambda<Func<TEntity, TEntity>>(
		Expression.MemberInit(Expression.New(typeof(TEntity).GetConstructors().Single()), bindings), param);

	await UpdateEntity(predicate, updateExpr);
}

Let me check in a hour.