Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BulkInsertOrUpdate throws "Cannot update identity column" exception #131

Closed
ssujithsnair opened this issue Jan 4, 2019 · 2 comments
Closed
Labels

Comments

@ssujithsnair
Copy link

ssujithsnair commented Jan 4, 2019

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 though

I 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.

@borisdj
Copy link
Owner

borisdj commented Jan 5, 2019

You should also set PropertiesToExclude with ModelId, so

var updateByProp = new List<string> { nameof(Model.ModelDate), nameof(Model.EntityId), nameof(Model.EntityTypeId), nameof(Model.DataTypeId) };
var propToExclude = new List<string> { nameof(Model.ModelId) };
var config = new BulkConfig { UpdateByProperties = updateByProp, PropertiesToExclude = propToExclude };
context.BulkInsertOrUpdate(modelList, config);

Additinaly you don't need any of the PK config since EF by convention makes Identity column if one is named Id/ID or TableNameId/ID.
So Attributes [Key], [DatabaseGenerated] can be removed as well as code from builder regarding the same.

PS
When adding code snippets into the comment use code formatting segment for C# for better readability.
```C#
// code goes here
```
I have edited(formatted) your question accordingly.

@ssujithsnair
Copy link
Author

That worked, Appreciate the help, Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants