question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Updating fails when using an identity column that is not a key

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
malhaar-pcommented, Jul 31, 2018

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.

0reactions
bricelamcommented, Jun 12, 2017

Yep, I just saw that. 😄 …catching up after being heads down last week…

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found