Note that there are some explanatory texts on larger screens.

plurals
  1. POPerformant for bulk insert of objects with child objects
    primarykey
    data
    text
    <p>I am currently in a struggle here. </p> <p><strong>Environment:</strong> I am <em>improving</em> an application in C# 4.0 wit MSSQL Server 2008 R2. I am not using any type of ORM.</p> <p><strong>Design/Model:</strong> (Fictive) I have a Conversation, a message, and a attachment. The conversation has multiple messages, each message can have multiple attachments. So easy 1 .. N connections over there.</p> <p><strong>The problem:</strong> I would like to store into my DB a new conversation, with new conversation messages and new attachments. My situation right now is that I go top-down by saving the conversation. Retrieve that id, set that id to the message objects and store the messages (every time I do separate db calls). And same story again for all the attachments, I need first to store the message before I can store these attachments.</p> <p><strong>My solutions I looked into or have in mind</strong></p> <ul> <li><p>Currently I do these steps individual, top-down saving mechanism through Stored Procedures.</p></li> <li><p>SqlBulkCopy : Really fast. I had in mind, store all conversations, get Ids back, store all messages, get their ids back etc... But the issues is here, I need to do a query like "Give me all 20 last created records their id". Doable, but the fact that this application is multithreaded it might be that in that moment other threads would have inserted new rows or worse, deleted.</p></li> <li><p>Create Stored Procedure a crazy stored procedure where I send in a dataset object (this is more free brainstorming thing).</p></li> <li><p>Manually generate a huge sql command that stores a conversation, gets the generated id, and uses that for the child objects. Like (pseudo SQL):</p> <p>INSERT conversation</p> <p>currentConversationId = @Identity</p> <p>INSERT message with conversationId = currentConversationId </p></li> </ul> <p><strong>Goal</strong> Does someone have better suggestions or alternatives. Please avoid advising me to go to an ORM because I am working with a legacy project. I would like to SAVE DB calls as much as possible.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload