Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linq2db .NET ORM partial updates?

Tags:

orm

linq2db

We use Linq2DB ORM library for our .NET ORM models see https://github.com/linq2db/linq2db

How to send update with only changed columns? Now the SQL query includes all of the columns and values

like image 742
sergpy Avatar asked Sep 18 '26 01:09

sergpy


2 Answers

Currently there is no mechanism for tracking changes in the object level to update only the changed properties but if you know what columns are changed, you can use the set function in conjunction with the update function to do the partial updates (just like the example given in the github page )

using (var db = new DbNorthwind())
{
   db.Product
    .Where(p => p.ProductID == product.ProductID)
    .Set(p => p.Name, product.Name)
   .Set(p => p.UnitPrice, product.UnitPrice)
  .Update();
}
like image 84
Guru Kathiresan Avatar answered Sep 21 '26 13:09

Guru Kathiresan


First of all, linq2db is a lightweight ORM. What means it doesn't have heavy context and it doesn't know anything about changes in entities and never will-if you want to have change tracking than use Entity Framework or NHibernate. So you have to implement change tracking yourself (if you really need it) or update the whole entity(like you did). If you know updated column you can use any syntax provided (see Linq2Db crud operations). If you know only changed column names-you can dynamically construct linq expressions and use one of syntaxes Linq2Db crud operations.

like image 24
Andrey Efimov Avatar answered Sep 21 '26 13:09

Andrey Efimov