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

时间:2016-02-08 16:02:20

标签: c# entity-framework

我正在尝试插入一个新的ActivityInstance,我没有设置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; }

现在我收到了错误:

  

ReferentialConstraint中的依赖属性映射到存储生成的列。专栏:'Id'。

我正在考虑目前可选择的一对一关系。确认报告表是:

CREATE TABLE [dbo]。[AcknowledgeReports] (     [ActivityInstanceId] INT NOT NULL,     CONSTRAINT [PK_AcknowledgeReportsId] PRIMARY KEY CLUSTERED([ActivityInstanceId] ASC) )

AcknowledgeReport模型

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

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

}

AcknowledgeReport mapping

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

        Property(t => t.ActivityInstanceId);


        // Relationships


    }

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

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

应该有

{{1}}

他的后脑勺会被击打。谢谢你的帮助。