无法将显式值插入标识列 INSERT_OFF

发布于 2024-11-02 14:25:10 字数 1181 浏览 0 评论 0原文

我已经在一个 ASP MVC 项目上工作了几个星期。我已经使用实体通过 linq 从数据库中插入和删除了内容,没有任何问题..直到现在。

我使用默认布局创建了我的项目。因此,在用户注册后,在 AccountController 上,我想在我的数据库中注册信息,

Client:
IdClient
Name
LastName

我通过表单请求此信息,然后执行以下操作:

Client client = new Client();
client.Name = model.Name;
client.LastName = model.LastName
storeDB.Clients.Add(client);
storeDB.SaveChanges();

Note that I'm not attempting to insert anything on the IdClient

The class looks like:
public class Client{

  [Key]
   public int IdClient {get; set;}
   public string Name {get; set;}
   public string LastName {get; set;}
}

我已插入其他表中,没有问题,但这个问题给了我一个: 当 IDENTITY_INSERT 设置为 OFF 时,无法在表“Client”中插入标识列的显式值。

异常详细信息:System.Data.SqlClient.SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表“Client”中插入标识列的显式值。

我检查了一下,桌子上的一切看起来都很正常,身份只是 IdClient!有人知道我为什么会得到这个吗? =SI不认为这与我在AccountsController中执行此插入的事实有任何关系,它没有意义......

更新:我插入到另一个表中并且它没有标记我那个错误,我不明白......我创建了它们相同!我创建了一个数据库,其中只有一个名为 CLIENT 的表,但它不起作用!有人知道我可以在哪里查找问题吗? =S

-I 不想 想要向标识列插入值,我尝试在显示表数据上添加新行,我手动向“名称”和“姓氏”引入值,单击“ENTER”,然后行已创建,但是当我通过查询执行此操作时,它不允许我这样做。

谢谢!

I have been working on an asp mvc project for a couple of weeks. I have inserted and deleted stuff from the database through linq using entities with no problem..until now.

I created my project with the default layout. So on the AccountController after the user is registered I want to register the info in my database

Client:
IdClient
Name
LastName

I ask for this info via a form, and do this:

Client client = new Client();
client.Name = model.Name;
client.LastName = model.LastName
storeDB.Clients.Add(client);
storeDB.SaveChanges();

Note that I'm not attempting to insert anything on the IdClient

The class looks like:
public class Client{

  [Key]
   public int IdClient {get; set;}
   public string Name {get; set;}
   public string LastName {get; set;}
}

I have inserted in other tables with NO problem but this one throws me a:
Cannot insert explicit value for identity column in table 'Client' when IDENTITY_INSERT is set to OFF.

Exception Details: System.Data.SqlClient.SqlException: Cannot insert explicit value for identity column in table 'Client' when IDENTITY_INSERT is set to OFF.

I checked and everything on the table looks normal, the identity is ONLY IdClient!! does anybody know why I'm getting this? =S I don't think it has anything to do with the fact that I'm doing this insert in the AccountsController, it doesn't make sense....

UPDATE: I inserted to another table and it doesnt mark me that error, i don't understand...I created them the same! I created a database with JUST One table called CLIENT, and it DOES NOT WORK! does anybody know where I can look for the problem? =S

-I don't want to insert a value to the identity column, i tried adding a new row on the show table data, i manually introduced values to Name and Last name, clicked ENTER and the row was created, but when I do it through a query it doesn't let me.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城 2024-11-09 14:25:10

检查映射类上的主键。它应该有一个类似这样的属性:

    [Column(Storage="_LetterOID", AutoSync=AutoSync.OnInsert, 
     DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]

重要的一点是 IsDbGenerate=true。如果您的属性缺少该属性,我们就发现了错误!如果您正在使用设计器,只需重新生成此类(通过删除设计器中的表并将其从服务器资源管理器中删除)。

这曾经为我解决过一​​次问题。

Check the primary key on the mapping class. It should have an attribute something like this:

    [Column(Storage="_LetterOID", AutoSync=AutoSync.OnInsert, 
     DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]

The important bit is the IsDbGenerated=true. If your attribute lacks that, we have found the error! If you're using the designer, just regenerate this class (by deleting the table in the designer and dropping it from the server explorer).

This solved the problem for me, once before.

樱花细雨 2024-11-09 14:25:10

正如安德烈所说,检查你的设计师。
编写两个表的脚本并确认有效的表确实有一个标识列。
尝试为两个插入运行探查器,它们插入的所有列是否相同?

需要更多信息来回答这个问题。

As Andrei says, check your designer.
Script out the two tables and confirm the table that works does indeed have an identity column.
Try running profiler for both inserts, are they inserting all the columns the same?

Need more info to answer this question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文