I have a BankReport entity class with 71 optional, one-to-one relationships. All of the 71 related entities are similar. They all have a PK / FK which is the ID of the parent BankReport.
If I create a BankReport entity and create the 71 child objects on it, when I call SaveChanges, there are 4 of the 71 that don't get inserted into the database. There are no errors and the rest of the entities save fine.
If however I leave enough (seems to be around 8) of the working relationships null, then the other 4 do get inserted. Likewise, if I only create those 4, they all get inserted.
It almost seems like I am hitting some sort of a limit, but that seems insane!
Here is the (abbreviated) BankReport entity
[Table("Reports", Schema = "Bank")]
[Index(nameof(BankId))]
public class BankReport
{
[Key]
[StringLength(30)]
public string BankReportId { get; set; }
public virtual BankCI CI { get; set; }
public virtual BankENT ENT { get; set; }
public virtual BankLEO LEO { get; set; }
...
}
Here is a sample child entity
[Table("RCM", Schema = "Bank")]
public class BankRCM : IBankData
{
[Key, ForeignKey("BankReport")]
[StringLength(30)]
public string BankReportId { get; set; }
public int? RCFD0426 { get; set; }
public int? RCFD2130 { get; set; }
public int? RCFD2150 { get; set; }
...
public virtual BankReport BankReport { get; set; }
}
Here is a configuration for the relationship
modelBuilder.Entity<BankReport>()
.HasOne(x => x.RCM)
.WithOne(x => x.BankReport)
.HasForeignKey<BankRCM>(x => x.BankReportId)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<BankReport>().Navigation(x => x.RCM).IsRequired();
modelBuilder.Entity<BankRCM>().Navigation(x => x.BankReport).IsRequired();
Finally, here's trivial example that demonstrates the issue
var modelContext = new CbdGeneralModelContext();
var bankReport = new BankReport
{
BankReportId = "B242-2022-Q4",
BankId = 242,
ReportPeriodId = "2022-Q4",
CreatedDateTime = DateTime.UtcNow,
ModifiedDateTime = DateTime.UtcNow
};
// Use reflection to populate all relationships on the bank report (if they inherit from IBankData)
foreach (var property in bankReport.GetType().GetProperties())
{
if (typeof(IBankData).IsAssignableFrom(property.PropertyType))
{
property.SetValue(bankReport, Activator.CreateInstance(property.PropertyType));
}
}
modelContext.BankReports.Add(bankReport);
modelContext.SaveChanges();
In the example above, SaveChanges completes without error. The BankReport record is in the database. Only 67 out of the 71 relationships on the BankReport are in the database. There seems to be nothing special or different about the 4 that never make it.
I have tried debugging into the tracked state of the entities before and after SaveChanges is called. Before SaveChanges, all 71 child entities are in the Added state. After SaveChanges is called, all 71 are in the Unchanged state. So it seems those 4 are being tracked. I've also debugged the state of the tracker and nothing looks off with those 4.
Finally, logging the SQL generated by Entity Framework shows that insert statements are never actually generated for the 4.
I am using EntityFrameworkCore 7.0.2
All of this is reproduceable every time I run.
Shortly after posting this I tried upgrading from EntityFrameworkCore 7.0.2 to 7.0.11. That seems to have fixed the issue! Crazy and hard to believe, but I'm super thankful!