无法从具有“身份"列的表中删除记录 [英] Cannot delete record from table which has Identity column

查看:94
本文介绍了无法从具有“身份"列的表中删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,其中的代码作为主键而不是ID,当我调用DeleteAsync方法时,出现异常Message = "Cannot update identity column 'Id'.".

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)
        {

        }
    }

但是,如果我从表中删除Id列,则可以正常工作.我想同时将Id列和Code列都作为PK.

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

推荐答案

建议我们应将Id用作PK,并将其他列设为唯一约束.因此,如果将Id用作PK,则不会遇到此类问题.

It is recommended that We should use Id as PK and make other columns as a unique constraint. So if you use Id as PK, you won't face such issues.

这篇关于无法从具有“身份"列的表中删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆