Base solution for your next web application
Open Closed

Cannot delete record from table which has Identity column #3864


User avatar
0
manojreddy created

I have a table which has the code as primary key instead of Id, When I call DeleteAsync method I get the exception Message = "Cannot update identity column 'Id'.".

[Table("Test")]

public class Test: FullAuditedEntity<int>

{

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
new public int Id { get; set; }
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public virtual int Code { get; set; }

public async Task DeleteTest(int code)
    {


        try
        {
            await _supplierRepository.DeleteAsync(p => p.Code== code);
        }
        catch (Exception ex)
        {

        }
    }

But If I remove Id column from the table, it works fine. I want both Id column and Code column as PK.

I have gone through some posts, there it is mentioned that primary key has to be Id. Why Abp framework enforces to use only ID as primary key, there has to be some strong reason behind it.


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @ManojReddy,

    Does this work when you first get the entity from DB using code field and delete using

    await _supplierRepository.DeleteAsync(testEntity);
    
  • User Avatar
    0
    manojreddy created

    Hi,

    Thanks for your reply.

    I'm not able to get the entity from DB as well, please share the code.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    It should be

    await _supplierRepository.FirstOrDefaultAsync(e => e.Code == code);