Since upgrading to .Net Core 3.1, one of my entities throws an error if I try to update it with the error 'cannot update identity column in Entity Framework Core'. This happens in the attached or the detached state.
Here is my fluent config
modelBuilder.Entity<OrderStatus>(b =>
{
b.ToTable("OrderStatus");
b.HasKey(e => e.OrderStatusId);
b.HasKey(e => new { e.CompanyId, e.OrderId }); //CompositeKey
b.Property(e => e.OrderStatusId).UseIdentityColumn();
});
I fetch the entity and update a value (not the OrderStatusId) and call save and thats when it blows up.
var orderStatus = _context.OrderStatus.FirstOrDefault();
orderStatus.Total = 500;
_context.Entry(OrderStatus).State = EntityState.Modified;
await _context.SaveChangesAsync();
I came across this old thread cannot update identity column in Entity Framework Core which mentions a 'fix' of adding
b.Property(e => e.OrderStatusId).Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Ignore);
This indeed does work, but I have no idea why this would be needed. This is the only Table in my application that seems to want this.
FYI, I was previous running on EF core 3.0 and tried upgrading to 3.1.3 but this makes no difference.
Should I be adding this to all of my fluent definitions now?
Suggestions appreciated!