简体   繁体   English

当IDENTITY_INSERT设置为OFF时,无法为表'ActivityInstances中的标识列插入显式值

[英]Cannot insert explicit value for identity column in table 'ActivityInstances' when IDENTITY_INSERT is set to OFF

I am trying to insert a new ActivityInstance, I am not setting the ID, but entity framework is attempting to insert an ID anyway. 我试图插入一个新的ActivityInstance,我没有设置ID,但是实体框架还是试图插入一个ID。

The model creation and save command: 模型创建和保存命令:

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

The Model 该模型

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; }
}

The Mapping 映射

    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);

    }

The table design 桌子设计

    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])
)

And this is what I am getting in my Output window. 这就是我在“输出”窗口中得到的。 The Id value is the default integer 0. Id值是默认整数0。

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

UPDATE I modified the ActivityInstance model with the first suggested answer which has led me toward the actual issue, though I still have not solved it. 更新我用第一个建议的答案修改了ActivityInstance模型,这使我走向了实际问题,尽管我仍然没有解决。 That recommendation was to modify the ActivityInstance model setting the below on id 该建议是修改在id上设置以下内容的ActivityInstance模型

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

Now I am getting the error: 现在我得到了错误:

A dependent property in a ReferentialConstraint is mapped to a store-generated column. ReferentialConstraint中的从属属性映射到商店生成的列。 Column: 'Id'. 列:“ Id”。

I am looking at the optional one to one relationship currently. 我目前正在查看可选的一对一关系。 Acknowledge report table is: 确认报告表为:

CREATE TABLE [dbo].[AcknowledgeReports] ( [ActivityInstanceId] INT NOT NULL, CONSTRAINT [PK_AcknowledgeReportsId] PRIMARY KEY CLUSTERED ([ActivityInstanceId] ASC) ) 创建表[dbo]。[AcknowledgeReports]([ActivityInstanceId] INT非空,约束[PK_AcknowledgeReportsId]主键聚集([ActivityInstanceId] ASC))

AcknowledgeReport model AcknowledgeReport模型

public class AcknowledgeReport : IActivityInstance
{
    public int ActivityInstanceId { get; set; }

    public virtual List<Report> Reports { get; set; }
    public virtual ActivityInstance ActivityInstance { get; set; }

}

And AcknowledgeReport mapping 和AcknowledgeReport映射

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

        Property(t => t.ActivityInstanceId);


        // Relationships


    }

Try adding the attributes to let it know that it is database generated 尝试添加属性以使其知道它是数据库生成的

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

Thanks to bhmahler's suggestion above I finally started looking into every relationship. 由于有了bhmahler的建议,我终于开始研究每一种关系。 One of the other programmers added the MsdsDataValidation and when they mapped the relationship they configured it backward. 其他程序员之一添加了MsdsDataValidation,并在映射关系时向后配置了该关系。 So in the MsdsDataValidationMap he had 因此,在MsdsDataValidationMap中,他有

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

and should have had 并且应该有

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

The back of his head will be smacked. 他的脑袋会被打。 Thanks for the help. 谢谢您的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架:当IDENTITY_INSERT设置为OFF时,无法为表&#39;[table]&#39;中的标识列插入显式值 - Entity Framework: Cannot insert explicit value for identity column in table '[table]' when IDENTITY_INSERT is set to OFF SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法为表 [表名] 中的标识列插入显式值 - SqlException: Cannot insert explicit value for identity column in table [Table name] when IDENTITY_INSERT is set to OFF 出现错误:当IDENTITY_INSERT设置为OFF时,无法在表&#39;Table&#39;中为标识列插入显式值 - Getting Error: Cannot insert explicit value for identity column in table 'Table' when IDENTITY_INSERT is set to OFF SqlException:当IDENTITY_INSERT设置为OFF时,无法为表&#39;Recipe&#39;中的Identity列插入显式值 - SqlException: Cannot insert explicit value for identity column in table 'Recipe' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,无法在表&#39;MyTableName&#39;中为标识列插入显式值 - Cannot insert explicit value for identity column in table 'MyTableName' when IDENTITY_INSERT is set to OFF SqlException:当IDENTITY_INSERT设置为OFF时,无法为表&#39;AspNetUsers&#39;中的Identity列插入显式值 - SqlException: Cannot insert explicit value for identity column in table 'AspNetUsers' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,无法在表&#39;candidatedetails&#39;中为identity列插入显式值 - Cannot insert explicit value for identity column in table 'candidatedetails' when IDENTITY_INSERT is set to OFF IDENTITY_INSERT设置为OFF时,EF无法为表“ PATIENT DONOR”中的标识列插入显式值 - EF Cannot insert explicit value for identity column in table 'PATIENT DONOR' when IDENTITY_INSERT is set to OFF IDENTITY_INSERT设置为OFF时,1:1映射并且无法为表&#39;DivisionParticipant&#39;中的Identity列插入显式值 - 1:1 mapping and Cannot insert explicit value for identity column in table 'DivisionParticipant' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,C#无法为表“出租”中的标识列插入显式值 - C# Cannot insert explicit value for identity column in table “Rentals” when IDENTITY_INSERT is set to OFF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM