当标识列设置为 OFF 时,无法在表“活动实例”中插入标识列IDENTITY_INSERT显式值

本文关键字:标识 插入 活动实例 IDENTITY INSERT 实例 OFF 设置 活动 | 更新日期: 2023-09-27 18:33:38

我正在尝试插入一个新的活动实例,我没有设置 ID,但实体框架无论如何都在尝试插入 ID。

模型创建和保存命令:

        var ai = new ActivityInstance
        {
            ActivityId = 1,
            LocationId = 1,
            BeginDate = DateTime.Now,
            ExpirationDate = DateTime.Now
        };
        DbContext.ActivityInstances.Add(activityInstance);
        DbContext.SaveChanges();

模型

public class ActivityInstance : IBusinessObject, IEntity
{
    public int Id { get; set; }
    public int LocationId { get; set; }
    public string TargetId { get; set; }
    public int ActivityId { get; set; }
    public DateTime? BeginDate { get; set; }
    public DateTime? ExpirationDate { get; set; }
    public Guid? DurableInstanceWorkflowId { get; set; }
    public int? TriggeredById { get; set; }
    public virtual Activity Activity { get; set; }
    public virtual Location Location { get; set; }
    public virtual MsdsDataValidation MsdsDataValidation { get; set; }
    public virtual ICollection<ResultsTransmittalQuestionAnswer> ActivityQuestionAnswers { get; set; }
    public virtual ICollection<ResultsTransmittalContact> ResultsTransmittalContacts { get; set; }
    public virtual Target Target { get; set; }
    public virtual CorrectiveActionPlan CorrectiveActionPlans { get; set; }
    public virtual FmVisit FmVisit { get; set; }
    public virtual AcknowledgeReport AcknowledgeReport { get; set; }
    public virtual SlCap SlCap { get; set; }
    public virtual FmVisitStudentRecordReview FmVisitStudentRecordReview { get; set; }
    public virtual ICollection<ActivityInstanceReminder> ActivityInstanceReminders { get; set; } 
   // public virtual InstancesTable Instances { get; set; }
}

映射

    public ActivityInstanceMap()
    {
        ToTable("ActivityInstances");
        HasKey(x => x.Id);
        Property(t => t.Id);
        Property(t => t.LocationId);
        Property(t => t.TargetId);
        Property(t => t.ActivityId);
        Property(t => t.BeginDate);
        Property(t => t.ExpirationDate);
        Property(t => t.DurableInstanceWorkflowId);
        Property(t => t.TriggeredById);
        HasOptional(x => x.Target)
            .WithMany(x => x.ActivityInstances)
            .HasForeignKey(x => x.TargetId);
        HasRequired(t => t.Activity)
            .WithMany(t => t.ActivityInstances)
            .HasForeignKey(t => t.ActivityId);
        HasRequired(t => t.Location)
            .WithMany(t => t.ActivityInstances)
            .HasForeignKey(t => t.LocationId);
        HasOptional(t => t.AcknowledgeReport)
            .WithRequired(t => t.ActivityInstance);
    }

桌子设计

    CREATE TABLE [dbo].[ActivityInstances]
(
    [Id] INT IDENTITY(1,1) NOT NULL, 
    [DurableInstanceWorkflowId] UNIQUEIDENTIFIER NULL,
    [TargetId] NVARCHAR(128) NULL,
    [LocationId] INT NOT NULL, 
    [ActivityId] INT NOT NULL, 
    [BeginDate] DATETIME NULL,
    [ExpirationDate] DATETIME NULL,
    [TriggeredById] INT NULL,
    CONSTRAINT [Pk_ActivityInstances_Id] PRIMARY KEY CLUSTERED ([Id] Asc), 
    CONSTRAINT [Fk_ActivityInstances_ToLocations] FOREIGN KEY ([LocationId]) REFERENCES dbo.Locations([Id]), 
    CONSTRAINT [Fk_ActivityInstances_ToActivities] FOREIGN KEY ([ActivityId]) REFERENCES dbo.Activities([Id]),
    CONSTRAINT [FkInstanceTriggeredInstance] FOREIGN KEY ([TriggeredById]) REFERENCES dbo.ActivityInstances([Id]),
    CONSTRAINT [Fk_ActivityInstances_ToDurableWorkflowInstance] FOREIGN KEY ([DurableInstanceWorkflowId]) REFERENCES [System.Activities.DurableInstancing].InstancesTable([Id])
)

这就是我在输出窗口中得到的。 Id 值为默认整数 0。

 INSERT [dbo].[ActivityInstances]([Id], [LocationId], [TargetId], [ActivityId], [BeginDate], [ExpirationDate], [DurableInstanceWorkflowId], [TriggeredById])
    VALUES (@0, @1, NULL, @2, @3, @4, NULL, NULL)

更新我用第一个建议的答案修改了 ActivityInstance 模型,这使我走向了实际问题,尽管我仍然没有解决它。 该建议是修改 ActivityInstance 模型,在 id 上设置以下内容

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

现在我收到错误:

引用约束中的依赖属性映射到存储生成的列。列:"Id"。

我目前正在研究可选的一对一关系。 确认报告表为:

创建表 [DBO]。[确认报告]( [ActivityInstanceId]整数不为空, 约束 [PK_AcknowledgeReportsId] 主键集群 ([ActivityInstanceId] ASC()

确认报告模型

public class AcknowledgeReport : IActivityInstance
{
    public int ActivityInstanceId { get; set; }
    public virtual List<Report> Reports { get; set; }
    public virtual ActivityInstance ActivityInstance { get; set; }
}

和确认报告映射

    public AcknowledgeReportMap()
    {
        ToTable("AcknowledgeReports");
        HasKey(t => t.ActivityInstanceId);
        Property(t => t.ActivityInstanceId);

        // Relationships

    }

当标识列设置为 OFF 时,无法在表“活动实例”中插入标识列IDENTITY_INSERT显式值

尝试添加属性以使其知道它是数据库生成的

[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

多亏了bhmahler上面的建议,我终于开始研究每一种关系。 其他程序员之一添加了 MsdsDataValidation,当他们映射关系时,他们向后配置了它。 所以在MsdsDataValidationMap中,他有

            HasOptional(t => t.ActivityInstance)
            .WithRequired(t => t.MsdsDataValidation);

并且应该有

            HasRequired(t => t.ActivityInstance)
            .WithOptional(t => t.MsdsDataValidation);

他的后脑勺会被打。 感谢您的帮助。