Skip to content
This repository has been archived by the owner on Dec 20, 2018. It is now read-only.

InvalidOperationException when adding multiple items to context #1073

Closed
CamiloTerevinto opened this issue Jan 7, 2017 · 5 comments
Closed

Comments

@CamiloTerevinto
Copy link

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:

public class MyBaseClass
{
    public ICollection<AnotherClass> AnotherClass { get; set; }
}

var myBaseClass = new MyBaseClass();
myBaseClass.AnotherClass = Enumerable.Range(0, 10).Select(i => new { AnotherClass { Number = i }).ToList();
context.MyBaseClass.Add(myBaseClass);

I get an InvalidOperationException upon adding the entity to the context:

InvalidOperationException: The instance of entity type 'AnotherClass' cannot be tracked because another instance of this type with the same key is already being tracked. When adding new entities, for most key types a unique temporary key value will be created if no key is set (i.e. if the key property is assigned the default value for its type). If you are explicitly setting key values for new entities, ensure they do not collide with existing entities or temporary values generated for other new entities. When attaching existing entities, ensure that only one entity instance with a given key value is attached to the context.

The same happens if I add one by one using a simple for loop.

@VahidN
Copy link

VahidN commented Jan 7, 2017

You are using at least 2 different contexts with different life times here.

  • How did you register your dbcontext at startup?
  • What is its life time (Scoped, Transient)?
  • How did you instantiate your context in the above sample, how did you get it? Does it come from the dependency injection or from the new DbContext directly?

@CamiloTerevinto
Copy link
Author

@VahidN

  1. It's registered as

    services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("MyContext")));

  2. I'd guess that's defined by AddDbContext

  3. I get the Context from DI and pass it to the function that generates the data:

    public MyContrroller(MyContext ctx) //plus many other services

    public static void AddData(this MyContext context)

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. Create A
  2. Create B using A
  3. Create C and add it to A
  4. Create multiple D and add it to C

1-3 always worked ok, not sure why 4 fails

@VahidN
Copy link

VahidN commented Jan 7, 2017

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

@CamiloTerevinto
Copy link
Author

@VahidN Using 1.1, according to NuGet the latest available. The models were scaffolded with the Scaffold-DbContext command

@CamiloTerevinto
Copy link
Author

Oh shit, I realized what's wrong by looking at #6490 as @VahidN linked.

The problem was the scaffold incorrectly generated this:

 entity.Property(e => e.AnotherClassId).ValueGeneratedNever();

Removing the line for both entities fixed the issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants