Skip to content

Better error message with ExecuteUpdate #37862

@MizardX

Description

@MizardX

What problem are you trying to solve?

When executing a ExecuteUpdateAsync and using some navigation property that is not included in the source query, I got a confusing error message.

List<int> deviationIds = [1, 2, 3];
await ctx.RingOperation
	.Where(ro => deviationIds.Contains(ro.Deviation!.ServerKey!.Value))
	.ExecuteUpdateAsync(setters => setters
		.SetProperty(ro => ro.DeviationId, ro => ro.Deviation!.ServerKey)
	);

This throws the exception

ArgumentException: Property 'System.Nullable`1[System.Int32] DeviationId' is not defined for type 'Microsoft.EntityFrameworkCore.Query.TransparentIdentifierFactory+TransparentIdentifier`2[RingOperation,Deviation]' (Parameter 'property')

I know now that I can rewrite the query into

await ctx.RingOperation
	.Where(ro => deviationIds.Contains(ro.Deviation!.ServerKey!.Value))
	.Select(ro => new { ro, ro.Deviation!.ServerKey })
	.ExecuteUpdateAsync(setters => setters
		.SetProperty(o => o.ro.DeviationId, o => o.ServerKey)
	); 

But from the error message, this is not at all obvious.

Minimal DbContext & Models
public partial class MyDbContext : DbContext
{
	public MyDbContext(DbContextOptions<MyDbContext> options)
		: base(options)
	{
	}

	public DbSet<Deviation> Deviation { get; set; } = default!;
	public DbSet<RingOperation> RingOperation { get; set; } = default!;

	protected override void OnModelCreating(ModelBuilder modelBuilder)
	{
		modelBuilder.Entity<Deviation>()
			.HasKey(d => d.DeviationID);

		modelBuilder.Entity<RingOperation>()
			.HasKey(ro => ro.RingOperationId);

		modelBuilder.Entity<RingOperation>()
			.HasOne(ro => ro.Deviation)
			.WithMany()
			.HasForeignKey(ro => ro.DeviationId)
			.HasPrincipalKey(d => d.DeviationID);
	}
}

public class Deviation
{
	public int DeviationID { get; set; }
	public int? ServerKey { get; set; }
}

public class RingOperation
{
	public int RingOperationId { get; set; }
	public int? DeviationId { get; set; }
	public Deviation? Deviation { get; set; }
}

Describe the solution you'd like

A better error message explaining that went wrong, and maybe a suggestion of how to fix it.

Metadata

Metadata

Assignees

No fields configured for Feature.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions