How can I exclude a property when using UpdateRange in Blazor Server application?

Cenk 986 Reputation points
2024-01-27T13:30:02.3666667+00:00

I am working on a Blazor Server application and want to know if there is a way to exclude a property when using the UpdateRange method. Below is the code I am using:

public async Task UpdateReportAsync(List<OrderDetail> reports)
{
    await using var ctx = await _db.CreateDbContextAsync();
    ctx.UpdateRange(reports);
    await ctx.SaveChangesAsync();
}

Any help or guidance is greatly appreciated. Thanks in advance!

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
719 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,477 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,577 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 60,376 Reputation points
    2024-01-27T17:34:57.43+00:00

    As you are using blazor, which is statefull, you could use change tracking rather than marking the whole entity modified.

    for your current just mark the property as unmodified.

    ctx.Entry(report).Property(x => x.Column ).IsModified = false;

    you will need to loop thru reports