如何使Entity Framework 6使用SQL SEQUENCE中的默认值

时间:2016-03-14 13:53:52

标签: c# sql-server entity-framework entity-framework-6

我创建了以下SQL SEQUENCE

# Initialize df
df <- data.frame(matrix(NA, nrow = length(dates), ncol = 5)) 
# Define names of cols and add column
names(df) <- c("Date", "Area", "Application", "Type", "Reading")
df$Date <- dates

在此表格中使用的是允许&#39; Consecutivo&#39;字段使用SEQUENCE

CREATE SEQUENCE SEQ_ORDENES_TRABAJO as int START WITH 1 INCREMENT BY 1 MINVALUE 1 CYCLE ;

这是与该表匹配的Entity Framework类

CREATE TABLE [adm].[OrdenesTrabajo](
    [Id] [uniqueidentifier] NOT NULL,
    [Consecutivo] [int] NOT NULL,
    [FechaIngreso] [datetime] NOT NULL,
    [RemolcadorId] [uniqueidentifier] NOT NULL,
    [Justificacion] [nvarchar](1000) NOT NULL,
    [Prioridad] [smallint] NOT NULL,
    [EstadoMantenimientoId] [uniqueidentifier] NOT NULL,
    [Usuario] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_adm.OrdenesTrabajo] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [adm].[OrdenesTrabajo] ADD  CONSTRAINT [OT_consecutivoConstraint]  DEFAULT (NEXT VALUE FOR [SEQ_ORDENES_TRABAJO]) FOR [Consecutivo]
GO

当我使用省略&#39; Consecutivo&#39; INSERT命令中的字段按预期工作,并从SEQUENCE中获取值。

如何让Entity Framework 6使用&#39; Consecutivo&#39;的默认值。插入实体时的列?

使用INSERT子句创建存储过程并从实体框架中调用它是唯一的选择吗?

1 个答案:

答案 0 :(得分:1)

原来我需要的是一行指定DatabaseGenerated

    [Required]
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public Int32 Consecutivo { get; set; }