gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:49:50 25 4
gpt4 key购买 nike

我创建了以下 SQL 序列

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

在此表中使用的是允许“Consecutivo”字段使用该 SEQUENCE

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

这是匹配该表的 Entity Framework 类

[Table("adm.OrdenesTrabajo")]
public class OrdenTrabajo
{
[Key]
public Guid Id { get; set; }

[Required]
public Int32 Consecutivo { get; set; }

[Required]
public DateTime FechaIngreso { get; set; }

[Required]
public Guid RemolcadorId { get; set; }

[Required]
[MaxLength(1000)]
public String Justificacion { get; set; }

[Required]
public Int16 Prioridad { get; set; }

[Required]
public Guid EstadoMantenimientoId { get; set; }

[Required]
public String Usuario { get; set; }

[ForeignKey("RemolcadorId")]
public Equipo Remolcador { get; set; }

[ForeignKey("EstadoMantenimientoId")]
public EstadoMantenimiento EstadoMantenimiento { get; set; }
}

当我使用在 INSERT 命令中省略“Consecutivo”字段的脚本时,它按预期工作并从 SEQUENCE 中获取值。

如何让 Entity Framework 6 在插入实体时使用“Consecutivo”列的默认值?

创建带有 INSERT 子句的存储过程并从 Entity Framework 调用它是我唯一的选择吗?

最佳答案

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

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

关于c# - 如何使 Entity Framework 6 使用 SQL SEQUENCE 中的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35989440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com