Try .NET /elinq/
Powered by Try .NET

SQL Server UPDATE

EF does a thorough work to track entity state. In cases where the fact of change is not clear, it's usually better to let EF to manage the update.

ELINQ (pure SQL) is preferred when we don't want to retrieve the entity or a bulk update is needed.

1) Update a single column in all rows example

var rows = DbContext.Database.Execute((Taxes taxes) =>
                UPDATE(taxes).SET(() => taxes.UpdatedAt = GETDATE()));

Console.WriteLine($"{rows} rows affected");

2) Update multiple columns example

var one = 0.01M;
var two = 0.02M;
var rows = DbContext.Database.Execute((Taxes taxes) =>
{
    UPDATE(taxes)
        .SET(() =>
        {
            taxes.MaxLocalTaxRate += two;
            taxes.AvgLocalTaxRate += one;
        });
    WHERE(taxes.MaxLocalTaxRate == one);
});

Console.WriteLine($"{rows} rows affected");

< BACK | HOME