工作单位SaveChanges问题

时间:2016-06-28 12:02:03

标签: asp.net-mvc c#-4.0 unit-of-work

出现此错误:

附加“Axp.Gcpt.Gpat.Model.WizardSelectionCriteriaStage”类型的实体失败,因为同一类型的另一个实体已具有相同的主键值。如果图中的任何实体具有冲突的键值,则在使用“附加”方法或将实体的状态设置为“未更改”或“已修改”时,可能会发生这种情况。这可能是因为某些实体是新的并且尚未收到数据库生成的键值。在这种情况下,使用“添加”方法或“已添加”实体状态来跟踪图表,然后根据需要将非新实体的状态设置为“未更改”或“已修改”。

我在我的应用程序中使用了工作单元。 我明白,如果我在3个表中插入数据,我将在第三次插入后有一个SaveChanges(),我不需要在每次插入后调用Savechanges三次。

现在我的问题是我从表中删除并插入到同一个表中。之后我调用Savechanges()。现在的问题是,一旦删除被执行,并且当它将超出插入行时,它将失败并出现重复主键错误。 我的理解是,一旦你调用Savechanges,它就会删除并插入。

代码

_unitOfWorkAsync.BeginTransaction();
var lookup = _lookUpSvc.GetLookupByCategory((int)LookupCategories.WizardType).ToList();

                    updatedWizardSelectionCriteriaStage.WizardText = wizardJsonData.ToString();
                    updatedWizardSelectionCriteriaStage.WizardTypeId = (int)WizardType.IncentiveWizard;
                    updatedWizardSelectionCriteriaStage.UpdatedBy = UserId;
                    updatedWizardSelectionCriteriaStage.UpdatedDate = CurrentDateTime;

            _wizardSelectionCriteriaStageSvc.Delete(updatedWizardSelectionCriteriaStage);
 var wizardSelectionCriteriaStage = new WizardSelectionCriteriaStage()
                    {
                        PricingId = currentStep.PricingId,
                        UserId = UserId,
                        WizardStepNumber =  wizard.CurrentStepIndex,
                        WizardText = wizardJsonData.ToString(),
                        WizardTypeId = (int)WizardType.IncentiveWizard,
                        IsActive = true,
                        CreatedBy = UserId,
                        CreatedDate = CurrentDateTime,
                        UpdatedBy = UserId,
                        UpdatedDate = CurrentDateTime
                    };
                    _wizardSelectionCriteriaStageSvc.Insert(wizardSelectionCriteriaStage);
 _unitOfWorkAsync.SaveChanges();
 _unitOfWorkAsync.Commit();

//映射类

public class WizardSelectionCriteriaStageMap : EntityTypeConfiguration<WizardSelectionCriteriaStage>
    {
        public WizardSelectionCriteriaStageMap()
        {
            // Primary Key
            this.HasKey(t => new { t.UserId, t.PricingId, t.WizardStepNumber,t.WizardTypeId});

            // Properties
            this.Property(t => t.UserId)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

            this.Property(t => t.PricingId)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

            this.Property(t => t.WizardStepNumber)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

            this.Property(t => t.WizardText)
                .IsRequired();

            // Table & Column Mappings
            this.ToTable("WIZ_SLCT_CRIT_STAGE");
            this.Property(t => t.UserId).HasColumnName("USER_ID");
            this.Property(t => t.PricingId).HasColumnName("PRC_ID");
            this.Property(t => t.WizardStepNumber).HasColumnName("WIZ_STEP_NO");
            this.Property(t => t.WizardText).HasColumnName("WIZ_TX");
            this.Property(t => t.WizardTypeId).HasColumnName("WIZ_TYPE");
            this.Property(t => t.IsActive).HasColumnName("ACT_IN");
            this.Property(t => t.CreatedBy).HasColumnName("CREAT_BY_USER_ID");
            this.Property(t => t.CreatedDate).HasColumnName("CREAT_TS");
            this.Property(t => t.UpdatedBy).HasColumnName("LST_UPDT_BY_USER_ID");
            this.Property(t => t.UpdatedDate).HasColumnName("LST_UPDT_TS");

            // Relationships
            this.HasRequired(t => t.User)
                .WithMany(t => t.WizardSelectionCriteriaStages)
                .HasForeignKey(d => d.UserId);
            this.HasRequired(t => t.Pricing)
                .WithMany(t => t.WizardSelectionCriteriaStages)
                .HasForeignKey(d => d.PricingId);
            this.HasRequired(t => t.Lookup)
                .WithMany(t => t.WizardSelectionCriteriaStages)
                .HasForeignKey(d => d.WizardTypeId);

            this.MapToStoredProcedures(e => e.Insert(v => v.HasName("usp_WizardSelectionCriteriaStage_Insert"))
              .Update(sp => sp.HasName("usp_WizardSelectionCriteriaStage_Update"))
              .Delete(sp => sp.HasName("usp_WizardSelectionCriteriaStage_Delete")));

        }
    }

//域类

 public class WizardSelectionCriteriaStage : BaseEntity
    {
        public int UserId { get; set; }
        public int PricingId { get; set; }
        public int WizardStepNumber { get; set; }
        public string WizardText { get; set; }
        public bool IsActive { get; set; }
        public int WizardTypeId { get; set; }
        public virtual User User { get; set; }
        public virtual Pricing Pricing { get; set; }
        public virtual LookUp Lookup { get; set; }
    }

0 个答案:

没有答案