Updating fails when using an identity column that is not a key
See original GitHub issueI define an entity like this:
public class Entity
{
public Guid ID { get; set; }
public int ClusteredId { get; set; }
public string SomeText { get; set; }
}
and a context like this:
public class SomeContext : DbContext
{
public DbSet<Entity> Entities { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Entity>(b => {
b.HasKey(x => x.ID).ForSqlServerIsClustered(false);
// With the following two lines the update FAILS
b.Property(x => x.ClusteredId).UseSqlServerIdentityColumn();
b.HasIndex(x => x.ClusteredId).IsUnique().ForSqlServerIsClustered();
//// With the following two lines the update works
//b.Property(x => x.ClusteredId).UseSqlServerIdentityColumn().ValueGeneratedOnAddOrUpdate();
//b.HasIndex(x => x.ClusteredId).IsUnique().ForSqlServerIsClustered();
//// With the following two lines the update works
//b.Property(x => x.ClusteredId).UseSqlServerIdentityColumn();
//b.HasAlternateKey(x => x.ClusteredId).ForSqlServerIsClustered();
});
}
}
If i use this context in the following way, it will fail on the second SaveChanges(), because EF will generate SQL to update the ClusteredId column.
Entity entity = new Entity { SomeText = "Some Text" };
context.Entities.Add(entity);
context.SaveChanges();
context.Entities.Update(entity);
context.SaveChanges();
exec sp_executesql N'SET NOCOUNT ON;
UPDATE [Entities] SET [ClusteredId] = @p0, [SomeText] = @p1
WHERE [ID] = @p2;
SELECT @@ROWCOUNT;
',N'@p2 uniqueidentifier,@p0 int,@p1 nvarchar(4000)',@p2='...',@p0=1,@p1=N'Some Text'
If, when defining the model, I either specify ValueGeneratedOnAddOrUpdate() for the ClusteredId property, or use an alternate key instead of a unique index on the same property, then the code will work, but in my oppinion either one of those two changes should not be neccessary.
Further technical details
EF Core version: 1.1.0 Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Windows 10 IDE: Visual Studio 2015
Issue Analytics
- State:
- Created 7 years ago
- Comments:13 (10 by maintainers)
Top Results From Across the Web
cannot update identity column in Entity Framework Core
Using 3.1, the key is to make sure the identity value of the object you are updating matches the value of the record...
Read more >How to resolve "Cannot update identity column 'id'" while ...
Method 1: The simplest way to resolve this is to delete the field causing the error from the map. If your map does...
Read more >Cannot update identity column 'Id'
I have a table that has an identity field named Id. This field is used for partitioning and is not the key of...
Read more >cannot update identity column in Entity Framework Core ...
using 3.1, the key is to make sure the identity value of the object you are updating matches the value of the record...
Read more >MERGE with IDENTITY_INSERT ON does not work if ...
Identity values cannot be updated. They can be inserted if IDENTITY_INSERT is turned on for that table. This code shows how that works:...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It is really good to update EF Core framework time by time rather updating too late.
I am using EF Core 2.1, I tried this.
builder.Property(e => e.ColumnName).Metadata.AfterSaveBehavior = PropertySaveBehavior.Ignore; It worked for me.
EF Core 2.1 is stable, and enough updates to cater most of the problems.
Yep, I just saw that. 😄 …catching up after being heads down last week…