-
Notifications
You must be signed in to change notification settings - Fork 862
InvalidOperationException when adding multiple items to context #1073
Comments
You are using at least 2 different contexts with different life times here.
|
I also tried adding the data directly to the context using a simple for-loop instead of to the parent itself, yet the same thing happens right upon adding the second element. More or less the structure is like this:
1-3 always worked ok, not sure why 4 fails |
Are you still using EF Core 1.0? EF Core 1.0 sets FKs by conventions when adding a key and it doesn't work for a PK in a one-to-many relationship. It's fixed in v1.1: dotnet/efcore#6490 |
@VahidN Using 1.1, according to NuGet the latest available. The models were scaffolded with the Scaffold-DbContext command |
Oh shit, I realized what's wrong by looking at #6490 as @VahidN linked. The problem was the scaffold incorrectly generated this:
Removing the line for both entities fixed the issue. |
Coming from StackOverflow
Decided to post it here as it is somewhat similar to #914.
The difference is that it is only one Task running that creates the entire object to add to the context.
I have a weird problem with Entity Framework that I've never seen before, nor I could find any information about it.
Given:
I get an InvalidOperationException upon adding the entity to the context:
The same happens if I add one by one using a simple for loop.
The text was updated successfully, but these errors were encountered: