SubSonic-3.0
SubSonic-3.0 copied to clipboard
Batch update causes error in SQLite
I am using SS 3.0.0.3 ActiveRecord (Current master build) with SQLite (I think this error might be reproducable in other databases also, although I didn't checked).
If I do something like this:
var customerRepo = Customer.GetRepo();
var customerList = customerRepo.GetAll().ToList();
And then after making changes to the list, try to do this:
customerRepo.Update(customerList);
It causes a SQLite error because if the customer is not changed (i.e. IsDirty flag is false) the sql is not empty but this:
UPDATE Customers
WHERE Id = @0;
UPDATE Customers
WHERE Id = @1;
If a check for IsDirty flag is added to method public int Update(IEnumerable<T> items, IDataProvider provider) at line 279 of SubSonic.Core\Repository\SubSonicRepository.cs, the error is resolved.
The resultant public int Update(IEnumerable<T> items, IDataProvider provider) method should be: public int Update(IEnumerable<T> items, IDataProvider provider) { BatchQuery bQuery = new BatchQuery(provider); int result = 0;
foreach(T item in items)
{
if (((IActiveRecord)item).IsDirty()) // adding this check fixes the problem...
{
var query = item.ToUpdateQuery(provider);
bQuery.Queue(query);
}
}
result = bQuery.Execute();
return result;
}
You can workaround this pretty simply:
customerRepo.Update(customerList.Where(customer => customer.IsDirty());
But it is still an error, isn't it? Or is was meant to be this way only?
Yep still an error, just a low priority one. Your fix would break batch updates for SimpleRepo and the Linq templates though.