Click here to Skip to main content
14,827,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to insert large records into table (purchases_item) using EF6 AddRange and return the IDs to use it using add sub table (stock_noserials).

Data Samples:
i have this gridview:
------ name --------- stock --------- price ---------- others columns...
------ item1 -------- 8     --------- 00000 ---------- others columns...
------ item2 -------- 5     --------- 00000 ---------- others columns...
------ item3 -------- 3     --------- 00000 ---------- others columns...


purchases_item table
-- ID ---- name --------- stock --------- price ---------- others columns...
--- 7 --   item1 -------- 8     --------- 00000 ---------- others columns...
--- 8 --   item2 -------- 5     --------- 00000 ---------- others columns...
--- 9 --   item3 -------- 3     --------- 00000 ---------- others columns...

stock_noserials table
-- ID -- purchases_item_ID -- name --------- stock --------- price ---------- others columns...
--- 1 -- 7                 -- item1 -------- 8     --------- 00000 ---------- others columns...
--- 2 -- 8                 -- item2 -------- 5     --------- 00000 ---------- others columns...
--- 3 -- 9                 -- item3 -------- 3     --------- 00000 ---------- others columns...


What I have tried:

now i use this Method:
i inserted all gridview rows to two list then to Database using this code and it work fine without IDs of the first table purchases_item

but
var DB_items = ConnectionTools.OpenConn();
           DB_items.purchases_item.AddRange(ImportedPurchasesItems);
           DB_items.SaveChanges();


           var DB_stocs = ConnectionTools.OpenConn();
           DB_stocs.stock_noserials.AddRange(ImportedStocks);
           DB_stocs.SaveChanges();


how i can return the Inserted IDs of purchases_item to edit the second list before inserted it like :

i tried to created foreach (i should make insert query for every row). but i noticed that it have more times.
Posted
Updated 30-Nov-20 21:48pm
v2
Comments
Gerry Schmitz 30-Nov-20 16:20pm
   
"Add range" is an illusion; underneath it's still one record at a time. Everyone wants it fast and cheap (but not good).

1 solution

Not clear without seeing your model, but the general trick is to use the navigation properties to associate the two entities. Entity Framework will then be able to automatically update the foreign key property with the generated ID of the inserted parent records.

The canonical example:
C#
public class Blog
{
    public int Id { get; set; }
    public string Title { get; set; }
    public IList<Post> Posts { get; set; }
}

public class Post
{
    public int Id { get; set; }
    public int BlogId { get; set; }
    public Blog Blog { get; set; }
    public string Title { get; set; }
}

context.Blogs.Add(new Blog
{
    Title = "New Blog",
    Posts = new List<Post>
    {
        new Post { Title = "Post 1" },
        new Post { Title = "Another Post" },
    },
});

context.SaveChanges();
Both Post instances will have the correct BlogId set when they are saved, even though the value isn't known until the Blog has been saved.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900