使用IDENTITY_INSERT与EF4 [英] Using IDENTITY_INSERT with EF4

查看:151
本文介绍了使用IDENTITY_INSERT与EF4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在具有预定义主键值的数据库中创建一个记录。我知道如何使用sql,但我想知道EF是否可以为我做这个?否则,我必须为插入创建一个存储过程。

I am trying to create a record in the db that has a predefined primary key value. I know how to do this with sql, but I was wondering if EF can do this for me? Otherwise, I will have to create a stored proc for the inserts.

推荐答案

如果你有 StoreGeneratedPattern 属性设置为您的实体的,您为其指定的实体密钥值将被传递到数据库。

In case you have the StoreGeneratedPattern attribute set to "None" for your entity, the Entity Key value you specify for it will be passed to database.

如果此属性设置为身份计算,则无法控制将在DB中为该列设置什么值

In case this attribute is set either to "Identity" or to "Computed" there is no way to control what value will be set to this column in DB.

所以,如果你的数据库中有一个自动递增的列,而ADO.NET实体数据模型向导已经设置了 StoreGeneratedPattern ,您可以使用XML编辑器在模型的SSDL部分手动将此属性更改为,然后启用 IDENTITY_INSERT

So, if you have an auto-incremented column in your database and the ADO.NET Entity Data Model wizard has set the StoreGeneratedPattern for you, you can manually change this attribute to "None" in the SSDL part of the model using XML Editor, and then enable IDENTITY_INSERT.

您可以使用 ObjectCo ntext.ExecuteStoreCommand 方法,如果您使用EF4或使用 ObjectContext.Connection.StoreConnection 属性来执行SQL命令 SET IDENTITY_INSERT< Your_Table> ON (在EF1和EF v4中)。

You can use the ObjectContext.ExecuteStoreCommand method if you are using EF4 or use the ObjectContext.Connection.StoreConnection property to execute a SQL command SET IDENTITY_INSERT <Your_Table> ON (both in EF1 and EF v4).

这篇关于使用IDENTITY_INSERT与EF4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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