SBFrancies
SBFrancies

Reputation: 4230

Entity Framework Core attempting to update column marked as Identity in model

I have a database table where one of the columns is an IDENTITY column (not the primary key which is set manually). My EF Core model looks like this:

public class Model
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }

    ...Other fields...

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int AnotherId { get; set; }
}

Despite the annotation, when I do an update on the table I find that the SQL which is generated tries to update the IDENTITY column:

UPDATE [Model] SET ... [AnotherId] = @p10 ...
WHERE [Id] = @p29;
SELECT @@ROWCOUNT;

This obviously fails, causing the an exception to be thrown. I am using Entity Core 2.0 and upgrading to 2.1 is currently not an option. Any ideas why the data annotation is being ignored?

Upvotes: 3

Views: 6213

Answers (3)

Nugsson
Nugsson

Reputation: 324

The solution for me in EF Core 3.1 was this:

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<YourTableName>().Property(c => c.YourPropertyName).UseIdentityColumn().ValueGeneratedOnAddOrUpdate();
    }

Without ValueGeneratedOnAddOrUpdate() I was seeing the same error as the OP when updating existing records. It looks therefore as though UseIdentityColumn() is equivalent to calling ValueGeneratedOnAdd().

Upvotes: 5

Salah Akbari
Salah Akbari

Reputation: 39946

Try Fluent API in OnModelCreating of the Context class. Something like this:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder
    .Entity<Model>()
    .Property(c => c.AnotherId )
    .UseSqlServerIdentityColumn();
}

Also in EF Core 2 it should be something like this:

modelBuilder.Property(c => c.AnotherId).UseSqlServerIdentityColumn();
modelBuilder.Property(p => p.AnotherId )
            .Metadata.AfterSaveBehavior = PropertySaveBehavior.Ignore;

Upvotes: 5

mjwills
mjwills

Reputation: 23820

The attribute you likely want to use is:

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]

as per the docs.

Upvotes: 1

Related Questions