How to: Bulk Update Artificial Types
Bulk updating objects of artificial types is the same as bulk updating objects of regular entity types.
A good approach for working with bulk operations and artificial types is to take advantage over Dynamic LINQ.
In order to perform a bulk update operation on objects of artificial type, you can use the following workflow:
To use the UpdateAll() method you need to add a using / Imports clause to the Telerik.OpenAccess namespace in your code.
- Define a query that will be the source for the update operation
-
Perform the operation
using System.Linq.Dynamic; using Telerik.OpenAccess; using (FluentModel context = new FluentModel()) { int artificialUpdated = context.GetAll("Car").Where("CarYear < {0}", 1990). UpdateAll(u => u.Set(o => o.FieldValue<string>("TagNumber"), o => o.FieldValue<string>("TagNumber") + "Old Reg")); }
Due to limitations in VB the extension methods on objects need to be called as static methods.
In VB, the UpdateAll() method call requires Option Strict = ON either for the whole project or for the file that calls the method.
Option Strict On Imports System.Linq.Dynamic Imports Telerik.OpenAccess Using context = New FluentModel() Dim artificialUpdated = context.GetAll("Car").Where("CarYear < {0}", 1990) _ .UpdateAll(Function(u) u.Set(Function(o) ExtensionMethods.FieldValue(Of String)(o, "TagNumber"), _ Function(o) ExtensionMethods.FieldValue(Of String)(o, "TagNumber") + "Old Reg")) End Using
The first parameter of the lambda expression in Set holds the old value and the second one is the new value of the property. The number of the Set calls depends on the number of the object's properties that you want to update.
The result from this query would be that the tag numbers of all cars produced before 1990 will be appended with "Old Reg". If the update fails at some point, the method will throw an exception and any changes that might have been done during the transaction would be rolled back.
Optionally, you can adjust the command timeout on bulk operation level as described in How to: Set The Timeout / On Query Level, if the execution of the commands it contains is expected to take long time. By default, it is the one specified in Runtime Configuration. If the command exceeds the timeout it will be terminated and a backend specific exception will be thrown.