How can I set a many-to-many EntityCollection in Entity Framework efficiently?

No EF will not defer any query execution. There is nothing like insert from select. Linq-to-entities is just query language and responsibility of query is to execute.It is strictly separated from persistence functionality offered by EF itself If you want to create relations between existing item from table1 and exiting items from table2 you can use code like this: using (var ctx = new YourContext()) { var table1 = new Table1 { Id = 123 }; ctx.

Table1s. Attach(table1); foreach (var table2 in table2sToInclude. Select(id => new Table2 { Id = id })) { ctx.

Table2s. Attach(table2); order. Table2s.

Add(table2); } ctx.SaveChanges(); } This code creates relation between Table1's item with id 123 and all Table2's items from table2sToInclude without loading any single record from the database What makes adding records one by one "lame"? Do you understand what is benefit of AddRange? AddRange in typical collection extends capacity of internal array and just copy items to extended array EntityCollection is not typical array and it must process each added entity.

So even if there will be some AddRange it will internally iterate items and process them on by one.

No EF will not defer any query execution. There is nothing like insert from select. Linq-to-entities is just query language and responsibility of query is to execute.It is strictly separated from persistence functionality offered by EF itself.

If you want to create relations between existing item from table1 and exiting items from table2 you can use code like this: using (var ctx = new YourContext()) { var table1 = new Table1 { Id = 123 }; ctx. Table1s. Attach(table1); foreach (var table2 in table2sToInclude.

Select(id => new Table2 { Id = id })) { ctx. Table2s. Attach(table2); order.

Table2s. Add(table2); } ctx.SaveChanges(); } This code creates relation between Table1's item with id 123 and all Table2's items from table2sToInclude without loading any single record from the database. What makes adding records one by one "lame"?

Do you understand what is benefit of AddRange? AddRange in typical collection extends capacity of internal array and just copy items to extended array. EntityCollection is not typical array and it must process each added entity.

So even if there will be some AddRange it will internally iterate items and process them on by one.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions