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

Cenk 1,026 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.
777 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,664 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.
11,292 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 71,506 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


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.