Cannot insert explicit value for identity column in table 'ClientDetails' when IDENTITY_INSERT is set to OFF

你。 提交于 2019-11-30 18:45:45

Add IsDbGenerated=true To ClientNo Property

[Column(Storage="_ClientNo", DbType="Int NOT NULL", **IsDbGenerated=true,** IsPrimaryKey=true, UpdateCheck=UpdateCheck.Never)] 
public int ClientNo
Jodrell

You cannot supply values to be inserted into an Identity column as part of an insert. They are automatically generated as an atomic part of the INSERT operation. This can be temporaily disabled on a per table basis with the SET IDENTITY_INSERT command.

Either, you should alter the database shcema but I do not reccomend this or better, you should not supply a value, or supply a NULL value for the Client_NO column in your insert.

In SQL you can use the SCOPE_IDENTITY() function to get the auto genrated value. the model will be automatically updated with this value.

Use archil's answer to tell the model not pass the this column in the generated select.

[Column(Storage="_ClientNo",
    DbType="Int NOT NULL", 
    IsDbGenerated=true,
    IsPrimaryKey=true, 
    UpdateCheck=UpdateCheck.Never)]  
public int ClientNo

Note the edition of IsDBGenerated to the arrtributes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!