-
-
Couldn't load subscription status.
- Fork 622
Closed
Labels
Description
Below code (simulated to show relevant parts) throw "Cannot update identity column 'ModelID'" exception.
class Model
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column("ModelID")]
public int ModelId { get; set; }
public DateTime ModelDate { get; set; }
public int EntityId { get; set; }
[Column("EntityTypeID")]
public int EntityTypeId { get; set; }
[Column("DataTypeID")]
public int DataTypeId { get; set; }
// More fields
}Context
builder.Entity<Model>(entity =>
{
entity.HasKey(p => p.ModelId);
entity.Property(p => p.ModelId).UseSqlServerIdentityColumn();
});Insert/Update code
var updateByProperties = new List<string> { nameof(Model.ModelDate), nameof(Model.EntityId), nameof(Model.EntityTypeId), nameof(Model.DataTypeId) };
context.BulkInsertOrUpdate(modelList, new BulkConfig { UpdateByProperties = updateByProperties } });
// Properties given in UpdateByProperties should be used to match records. It's unique key on table.
// The inserted entries have ModelId set to zero
// BulkInsertOrUpdate (Or BulkUpdate) throws "Cannot update identity column 'ModelID'"I also tried below as well based on certain suggestions I saw regarding this topic for Entity framework core - but didn't help
entity.HasAlternateKey(x => x.ModelId).HasName("ModelID");
entity.Property(p => p.ModelId).ValueGeneratedOnAdd(); // this should have same effect as UseSqlServerIdentityColumn thoughI also tried SetOutputIdentity, KeepIdentity even though it didn't look like related to the issue.
If I do not include UpdateByProperties, it doesn't throw any exception but it doesnt work as expected.
BulkUpdate does nothing while BulkInsertOrUpdate does an insert which is not the expectation.