-
-
Notifications
You must be signed in to change notification settings - Fork 580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BulkRead erroring with " An item with the same key has already been added" on one-to-many relation #733
Comments
Interesting use case, or so to say problem. TableInfo tableInfo = TableInfo.CreateInstance(context, typeof(Package), new List<Package>(), OperationType.Insert, null);
var db = context.Database;
db.ExecuteSqlRaw(SqlQueryBuilder.CreateTableCopy(tableInfo.FullTableName, tableInfo.FullTempTableName, tableInfo));
db.ExecuteSqlRaw(SqlQueryBuilder.AlterTableColumnsToNullable(tableInfo.FullTempTableName, tableInfo));
string destinationTableName = tableInfo.FullTempTableName.Replace("[", "").Replace("]", "");
context.BulkInsert(packageAssignmentIds, new BulkConfig() { CustomDestinationTableName = destinationTableName });
string pkColumn = nameof(Package.Id);
string joinColumn = nameof(PackageAssignment.PackageId);
var records = context.Items.FromSqlRaw(
$"SELECT * FROM {tableInfo.FullTableName}" +
$"WHERE {pkColumn } IN " +
$"(SELECT {joinColumn} FROM {tableInfo.FullTempTableName})"
).AsNoTracking().ToList();
db.ExecuteSqlRaw(SqlQueryBuilder.DropTable(tableInfo.TempTableName, false)); PS More related infos: IN() list queries |
Thank you @borisdj we'll give it a shot |
@borisdj any plans to make this extensible and a permanent part of the library's API? I found this library which would be an improvement but I think the approach of using a bulk inserted temp table would yield better results than he has graphed |
Did not planned it, but can consider. I guess what could be done, just for simple case (support for only one type of child), is to encapsulate this in a method If you are up for a challenge to try implement it and make a PR. |
@borisdj I will see if I can properly submit a PR, not sure I have the interior knowledge of the library required but this is the extension method we came up with that works quite well for our use case,
EFCore 6.0 Contains
BlazarTech.QueryableValues
BulkReadWhereContainsAsync
Test 1 Test 2 Test 3 |
Will see if it can be added / integrated into library. |
This can now be done directly using config ReplaceReadEntities:
|
Hi @borisdj
We are attempting to implement bulk services to read a parent table by Id, then read the child relation (one-to-many) by the joining/navigational property of the parent id, then assign those child properties to the parent to return the complete object.
The problem is the child relation, is a one-to-many and can be assigned to many of the parent Entity (Package)
System.Private.CoreLib: Exception while executing function: Package-Reorder. System.Private.CoreLib: An item with the same key has already been added. Key: 19.
This is the error we are encountering, is there a work-around for this other than querying for the ids of the child table with a where contains and then materializing the child query?
This is what we are using that triggered the error,
Entities below,
The text was updated successfully, but these errors were encountered: