Bulk save all child objects in BusinessListBase #2828
-
Hello, Is there a way to bulk save all child objects in a list? Using dbconnection and dbcommand or EntityFramework. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Yes, there are various ways to do this. I think some might be discussed in the Using CSLA 4 book. The basic idea is this: the parent object (the list) has a default implementation for updating its children, and you can override that method. You basically need to do the same thing the default implementation does, but you can do it differently. The default implementation loops through the list of deleted items and asks each child to delete itself, then they are removed from the deleted list. It then loops through the list of active items and asks each child to update itself (if IsDirty is true), or insert itself (if IsNew is true). Normally you put the code to insert/update/delete a child in the child class's child data portal operation methods, so each child object can do its task when asked by the parent. However, you can put empty implementations of the insert/update/delete operations in the child class (as they still need to be called - just do nothing). In your override of the update children method in your list class, you do sort of the same thing:
The other bit of complexity to deal with, is if inserting a child results in a new db identity value for the child, you'll need to get those new identity values and figure out how to update the correct in-memory objects with their new property values. |
Beta Was this translation helpful? Give feedback.
-
Hi @RocketLabsDev we have done this with a batch processor class to automagically generate SQL statements for a In your DbContextBatchProcessor m_batchProcessor = null;
public DbContextBatchProcessor BatchProcessor
{
get
{
if (m_batchProcessor == null)
m_batchProcessor = new DbContextBatchProcessor(this);
return m_batchProcessor;
}
} In the root object you create a In the root call For some scenarios the performance gain is massive, as you're only doing a handful of round-trips instead of thousands. If anything is unclear, let me know. |
Beta Was this translation helpful? Give feedback.
Yes, there are various ways to do this. I think some might be discussed in the Using CSLA 4 book.
The basic idea is this: the parent object (the list) has a default implementation for updating its children, and you can override that method. You basically need to do the same thing the default implementation does, but you can do it differently.
The default implementation loops through the list of deleted items and asks each child to delete itself, then they are removed from the deleted list.
It then loops through the list of active items and asks each child to update itself (if IsDirty is true), or insert itself (if IsNew is true).
Normally you put the code to insert/update/delete a child i…