在实体框架,SQL Server 2008中打开IDENTITY_INSERT吗? [英] Turn on IDENTITY_INSERT in Entity Framework, SQL Server 2008?

查看:96
本文介绍了在实体框架,SQL Server 2008中打开IDENTITY_INSERT吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何在SQL Server 2008中打开IDENTITY_INSERT?

尝试通过实体框架将数据插入SQL Server 2008数据库。
我收到错误

Trying to insert data via entity framework into a SQL Server 2008 database. I get error


IDENTITY_INSERT设置为OFF

IDENTITY_INSERT is set to OFF


来自应用程序的

from the application.

我已经运行查询 SET IDENTITY_INSERT数据库。 dbo。启用购物篮

并返回消息成功完成命令。但是错误仍然发生...?

And got the message back Command(s) completed successfully. But error still occur...?

storeDB.Baskets.Add(cartItem);          
storeDB.SaveChanges();

出现在 storeDB.SaveChanges();

推荐答案

使用标识列时,需要确保已在EDMX(实体框架)模型中进行了适当设置。您的列应如下所示:

When using an identity column, you need to make sure you have set this appropriately in your EDMX (Entity Framework) model. Your column should look like this:

您的列是否设置为 StoreGeneratedPattern = Identity Entity Key = True ?如果不是,请尝试!

Is your column set to be StoreGeneratedPattern = Identity and Entity Key = True ?? If not - try that!

这是在创建EDMX模型时存在这种情况时应自动映射的默认值。如果在创建模型后在SQL Server中更改列,但没有更新模型,则可能存在差异,可能导致此类错误。

That's the default that should be mapped automatically, when that situation existed at the time you created the EDMX model. If you changed your column in SQL Server after you've created the model and you didn't update the model, then you might have a discrepancy that could lead to such errors.

这篇关于在实体框架,SQL Server 2008中打开IDENTITY_INSERT吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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