-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
System.InvalidOperationException after upgraded to EF Core 3.1.2 #20065
Comments
This is probably a duplicate of #18007 as you are populating EF Core also doesn't work well with singletons. As a workaround populate |
Are you suggesting something like this? This still throws the same exception. public class Job : Entity
{
public int OrderId { get; private set; }
public string JobNumber { get; private set; }
public JobStatus JobStatus
{
get => JobStatus.From(_jobStatusId);
private set => _jobStatusId = value.Id;
}
private int _jobStatusId;
public Job()
{
JobNumber = $"Job_{Guid.NewGuid()}";
_jobStatusId = JobStatus.Draft.Id;
}
}
i am not quite sure what you mean here. We are already doing Many thanks |
No, I mean context.AttachRange(JobStatus.List()); And you can remove |
Thanks @AndriySvyryd, Attaching Enumeration-based classes |
We are getting System.InvalidOperationException after upgraded to EF Core 3.1.2. It was working perfectly fine on EF Core 2.2 without any issue.
To setup the scene here:
In our application, we have orders, order contains multiple jobs and job has different job status. We use code first migration and utilised enumeration classes instead of enum types which is suggested by .NET microservices architecture e-book: https://docs.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/enumeration-classes-over-enum-types
The symptom:
When adding a new job to the order that has already had a job whose job status is the same as the one about to be added, we will get the below exception:
System.InvalidOperationException: The instance of entity type 'JobStatus' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.
Steps to reproduce
To simply things I have created a sample project to reproduce the problem: https://github.com/cobyzeng/EFCoreIssues
Exceptions:
Additional Information
If I put a breakpoint at line
await _context.SaveChangesAsync(true);
in OrderRepository and inspect the ChangeTracker, I can see the ChangedCount is 0, which i would expect not 0.If I call
_context.ChangeTracker.DetectChanges();
before saving change, the same exception gets thrown from there.Further technical details
EF Core version: 3.1.2
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: .NET Core 3.1
IDE: (e.g. Visual Studio 2019 16.3)
The text was updated successfully, but these errors were encountered: