{"action":"created","issue":{"url":"https://api.github.com/repos/FransBouma/LinqToSQL2/issues/15","labels_url":"https://api.github.com/repos/FransBouma/LinqToSQL2/issues/15/labels{/name}","comments_url":"https://api.github.com/repos/FransBouma/LinqToSQL2/issues/15/comments","events_url":"https://api.github.com/repos/FransBouma/LinqToSQL2/issues/15/events","html_url":"https://github.com/FransBouma/LinqToSQL2/issues/15","id":53878841,"number":15,"title":"Batch inserts","user":{"login":"MikaelEliasson","id":888296,"avatar_url":"https://avatars.githubusercontent.com/u/888296?v=3","gravatar_id":"","url":"https://api.github.com/users/MikaelEliasson","html_url":"https://github.com/MikaelEliasson","followers_url":"https://api.github.com/users/MikaelEliasson/followers","following_url":"https://api.github.com/users/MikaelEliasson/following{/other_user}","gists_url":"https://api.github.com/users/MikaelEliasson/gists{/gist_id}","starred_url":"https://api.github.com/users/MikaelEliasson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MikaelEliasson/subscriptions","organizations_url":"https://api.github.com/users/MikaelEliasson/orgs","repos_url":"https://api.github.com/users/MikaelEliasson/repos","events_url":"https://api.github.com/users/MikaelEliasson/events{/privacy}","received_events_url":"https://api.github.com/users/MikaelEliasson/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-01-09T15:27:32Z","updated_at":"2015-01-11T08:37:29Z","closed_at":null,"body":"One cool thing EF7 will do is to use OUTPUT in Sql Server to support batch inserts even with db generated ids. I have not investigated if any other engines support it or have similar. \r\n\r\nCompared to SqlBulkCopy it's ofcourse slower, but the advantage is that it supports FK fixup automatically ang gives better error messages and might even be faster under a certain treshold. It already needs code to decide in which order a graph should be inserted so should be a quite easy implementation."},"comment":{"url":"https://api.github.com/repos/FransBouma/LinqToSQL2/issues/comments/69487424","html_url":"https://github.com/FransBouma/LinqToSQL2/issues/15#issuecomment-69487424","issue_url":"https://api.github.com/repos/FransBouma/LinqToSQL2/issues/15","id":69487424,"user":{"login":"MikaelEliasson","id":888296,"avatar_url":"https://avatars.githubusercontent.com/u/888296?v=3","gravatar_id":"","url":"https://api.github.com/users/MikaelEliasson","html_url":"https://github.com/MikaelEliasson","followers_url":"https://api.github.com/users/MikaelEliasson/followers","following_url":"https://api.github.com/users/MikaelEliasson/following{/other_user}","gists_url":"https://api.github.com/users/MikaelEliasson/gists{/gist_id}","starred_url":"https://api.github.com/users/MikaelEliasson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MikaelEliasson/subscriptions","organizations_url":"https://api.github.com/users/MikaelEliasson/orgs","repos_url":"https://api.github.com/users/MikaelEliasson/repos","events_url":"https://api.github.com/users/MikaelEliasson/events{/privacy}","received_events_url":"https://api.github.com/users/MikaelEliasson/received_events","type":"User","site_admin":false},"created_at":"2015-01-11T08:37:29Z","updated_at":"2015-01-11T08:37:29Z","body":"Even self referencing relationsships should be solvable. If it's worh it is another question though. \r\n\r\nBuilding a dependency graph should solve it in the generic case. It's a bit of work but should be very doable. If we assume the most common use case you will only have 1-M self refering relationsships (a hierarchy) and they Add() the items as they \"should\" \r\n\r\n```Parent { Children : [Child1 { Children : [Child1.1, Child1.2] }, Child2] }```\r\n\r\nThis is easily divided by using a series of collections. \r\n```\r\n1: Add all the 0 level items to Batch1\r\n2: Add all the children of items in Batch1 to Batch2\r\n3- (N-1): Repeat untill empty\r\nN: Insert B1, propagate the returned id's to the FK's in B2, repeat untill empty\r\n```\r\n\r\nN-M relationsships: are not a problem as all the items are can be inserted at once and the mapping table get inserted after.\r\n\r\n1-1 relationsships: Not even sure if L2S supports it. I don't think I used it. I think it can be done but if you insert both entities it would require inserting and then updating as far as I can see (should be same problem with single row).\r\n\r\nA more generic algorithm is needed if the user decides to insert like:\r\n\r\n```\r\nParent{ P1 }\r\nChild{ C1, Parent: P1 }\r\nChild{ C2, Parent: P1 }\r\n\r\nAdd(P1)\r\nAdd(C1)\r\nAdd(C2)  \r\n```\r\n\r\nI have to think about if I can find a simple and fast algorithm for that. I have a few ideas but need to test/google a bit.\r\n"}}