In my ASP.NET Core app I have DbContext that contains two types of Entities, say, Pupils and Classes. I want to add a new Class, get the Id of new Class and add a new Pupil to the class. I tried to do it this way:
var Class = new Class{ name = "Math" };
Context.Classes.Add(Class);
await Context.SaveChangesAsync();
var Pupil = new Pupil{ name = "Rem", ClassId = Class.Id };
Context.Pupils.Add(Pupil);
await Context.SaveChangesAsync();
However I got the error on the second call to SaveChangesAsync():
Microsoft.EntityFrameworkCore.DbContext:Error: An exception occurred in the database while saving changes. System.InvalidOperationException: A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.
I tried to wrap everything in transaction this way:
using (var transaction = Context.Database.BeginTransaction()) {
....
transaction.Commit();
}
But the problem persists.
What is the correct way to perform two related operations in EF Core? Is it possible to do in a single transaction?
PS: I'm using PostgreSQL with Npgsql provider
await Context.SaveChangesAsync()
and do something with it (like, return if it's zero)? Again, blind blind guess... – Andrés Robinet Dec 13 '16 at 6:56await
and you are using ASP.NET Core... meaning you are in a controller action when this code executes, right? – Andrés Robinet Dec 13 '16 at 6:57