Automatic population of an Identity ID property in async methods fails when saving in parent method.
This is a difficult bug to explain, but I have an example below that demonstrates what I saw. If I addAsync in a child method, then save in the parent, the auto-id isn't inserted into the object like it should be. I have no idea why, but it must be some crazy threading thing behind the scenes.
I'm sorry, but I don't have any more time now to enter this issue, and I do have a workaround. I just thought it was so freaking weird, that you should at least be made aware of it.
This code doesn't work as expected. The ceo.Id still that weird negative placeholder value.
private async Task DoStuff() {
using (var context = new MyContext()) {
var ceo = await CreateCeoAsync(context);
await context.SaveChangesAsync(); // If the save is here, the user will NOT the proper ID. It's negative below.
var company = new Company {
Name="microstuff",
CeoId=ceo.Id // Id is an identity column in SQL Server
};
}
}
private async Task<User> CreateCeoAsync(MyContext context) {
var user = new User {
Name="Poopy Smith"
};
await context.AddAsync(user);
return user;
}
This works as expected. The ceo.Id is properly populated.
private async Task DoStuff() {
using (var context = new MyContext()) {
var ceo = await CreateCeoAsync(context);
var company = new Company {
Name="microstuff",
CeoId=ceo.Id // Id is an identity column in SQL Server
};
}
}
private async Task<User> CreateCeoAsync(MyContext context) {
var user = new User {
Name="Poopy Smith"
};
await context.AddAsync(user);
await context.SaveChangesAsync(); // If the save is here, the user will get the proper ID
return user;
}
Further technical details
EF Core version: 2.1.2 Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: IDE: Visual Studio 2019 16.1.6
@justincts1975 I am not able to reproduce this on either 2.1 or 2.2 releases--see my code below. Please post a small, runnable project/solution or complete code listing like below that demonstrates the behavior you are seeing.
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public int CeoId { get; set; }
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Program
{
public static async Task Main()
{
using (var context = new BloggingContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
}
await new Program().DoStuff();
}
private async Task DoStuff() {
using (var context = new BloggingContext())
{
var ceo = await CreateCeoAsync(context);
await context.SaveChangesAsync(); // If the save is here, the user will NOT the proper ID. It's negative below.
var company = new Company {
Name="microstuff",
CeoId=ceo.Id // Id is an identity column in SQL Server
};
}
}
private async Task<User> CreateCeoAsync(BloggingContext context)
{
var user = new User
{
Name="Poopy Smith"
};
await context.AddAsync(user);
return user;
}
}
public class BloggingContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>();
modelBuilder.Entity<Company>();
}
}
Here's a video of it happening in real time: https://www.loom.com/share/29d815a4f7c44799b28f33604ac38887 I appreciate your time. I know it feels like people are wasting it when you have to look through bug reports all day!
You are using different contexts in your video. My guess is SaveChanges actually did not save anything. So ride.Id is not going to be changed. When you move the method, you are calling SaveChanges on same context which did AddAsync so record would be saved and PK value would be updated by EF.
You're right. I must not be getting enough sleep. Sorry for wasting your time. Next time I will post the video FIRST! ---- and I'll use this embarrassing situation to remind me that I should have someone else review the bug before reporting it. When you're in the 12 work hours a day zone you start to become a moronic zombie!