asp.net mvc - Unit of Work SaveChanges Issue -
getting error:
attaching entity of type 'axp.gcpt.gpat.model.wizardselectioncriteriastage' failed because entity of same type has same primary key value. can happen when using 'attach' method or setting state of entity 'unchanged' or 'modified' if entities in graph have conflicting key values. may because entities new , have not yet received database-generated key values. in case use 'add' method or 'added' entity state track graph , set state of non-new entities 'unchanged' or 'modified' appropriate.
i using unit of work mvc in application. understand if inserting data in 3 tables have 1 savechanges() after third insert, dont need call savechanges 3 times after each insert.
now problem deleting table , inserting in same table.after call savechanges(). problem once delete exectued , when going exceute insert line fails error of duplicate primary key. understanding once call savechanges delete , insert.
code
_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();
// mapping class
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"))); } }
//domain class
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; } }
Comments
Post a Comment