I have an entity with its primary key made up from two columns.
This is the model class:
public class DemoEntity
{
public Guid EntityId {get;set;} // randomly generated primary key
public int Id {get;set;}. //auto increment identity primary key
public string Description {get;set;}
}
I want to update couple of entities which I will be receiving from client. Client doesn't get EntityId
, so in payload we won't have it.
Payload will look like this:
[
{
"id": 1,
"description": "Updating entity with I'd =1"
}
]
Above payload can have maximum 6 object.
In order to update the database, I am using this code:
[HttpPut]
public Task<ActionResult> Update([FromBody] List<DemoEntity> entities)
{
_dbContext.UpdateRange(entities);
await _dbContext.SaveChangesAsync();
return No content();
}
This code is throwing an error while saving changes to the database:
An error occurred while saving the entity changes. See the inner exception for details.
Cannot insert non-DEFAULT value into column "id".
Column "id" is an identity column defined as GENERATED ALWAYS.
This error is getting fixed if I set EntityId
. But we don't want to expose the EntityId
to the client.
Is there any way to specify that which primary key EF core should use internally while updating the entity in case there are multiple primary keys are present?