在实体框架中使用序列作为主键值的生成器进行插入 [英] Insert using a Sequence as generator for a Primary Key value in Entity Framework
本文介绍了在实体框架中使用序列作为主键值的生成器进行插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的序列:
I have a sequence that looks like this:
CREATE SEQUENCE dbo.NextWidgetId
AS [bigint]
START WITH 100
INCREMENT BY 2
NO CACHE
GO
还有一个看起来像这样的表:
And a table that looks like this:
CREATE TABLE [dbo].[Widget_Sequenced]
(
[WidgetId] [int] NOT NULL DEFAULT(NEXT VALUE FOR dbo.NextWidgetId),
[WidgetCost] [money] NOT NULL,
[WidgetName] [varchar](50) NOT NULL,
[WidgetCode] [int] NOT NULL,
[LastChangedBy] [int] NOT NULL,
[RowVersionId] [timestamp] NOT NULL,
CONSTRAINT [PK_Widget_Sequenced]
PRIMARY KEY CLUSTERED ([WidgetId] ASC) ON [PRIMARY]
) ON [PRIMARY]
是否可以使用Entity Framework向该表结构添加新记录?
Is there a way to add a new record to this table structure using Entity Framework?
我尝试设置 StoreGeneratedPattern
用于 WidgetId
到计算
,我尝试了h 身份
。两者都给了我错误。
I tried setting StoreGeneratedPattern
for WidgetId
to computed
and I tried it with Identity
. Both gave me errors.
我使用EF 5进行了尝试。但是,如果它可以解决这个问题,我可以转向EF 6。
I tried this with EF 5. But I could move to EF 6 if it fixes this.
推荐答案
从6.2版开始,可以使用以下代码:
It's possible from version 6.2, using this code:
System.Data.Entity .SqlServer.SqlProviderServices.UseScopeIdentity = false;
这篇关于在实体框架中使用序列作为主键值的生成器进行插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文