Reputation: 4230
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
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
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