asp.net mvc 4 - Copy one form Data to another form Entity Framework -
in mvc4 web application have copy data of 1 form have been saved newly created form. when copy data using following code, data moves 1 form , data previous form deleted. please specify why happening , how copy data.
public formentity copyformdata(copyform copyform) { using (sipsentities db = new sipsentities()) { if (db.qmsevaluationforms.where(x => x.isactive == true).any(x => x.formname == copyform.formname)) { throw new exception("form name exists!"); } else { var copydata = (from log in db.qmsevaluationforms log.evaluationformid == copyform.copyfrom && log.isactive == true select new { zeroonfatal = log.makescorezeroonfatal, formcontrols = (from log1 in log.qmsevaluationformcontrols log1.isactive == true select log1).tolist(), formparameters = (from log1 in log.qmsevaluationparameters log1.isactive == true select log1).tolist() }).firstordefault(); //insert new form qmsevaluationform newform = new qmsevaluationform(); newform.formname = copyform.formname; newform.makescorezeroonfatal = copydata.zeroonfatal; newform.isactive = true; newform.createdon = datetime.now; newform.subprocessid = copyform.subprocessid; //(copy data child table) data deleted previous form , copied form newform.qmsevaluationformcontrols = copydata.formcontrols; //(copy data child table) data deleted previous form , copied form newform.qmsevaluationparameters = copydata.formparameters; db.qmsevaluationforms.add(newform); db.savechanges(); return new formentity { formid = newform.evaluationformid, formname = newform.formname, makezero = newform.makescorezeroonfatal }; } } }
i got solution of problem debugging code many times. happening here when copy child table data new form at
newform.qmsevaluationformcontrols = copydata.formcontrols;
and newform.qmsevaluationparameters = copydata.formparameters;
the foreign key of each row changed new form's primary key. when copying data following method, every thing working fine-
public formentity copyformdata(copyform copyform) { using (sipsentities db = new sipsentities()) { if (db.qmsevaluationforms.where(x => x.isactive == true).any(x => x.formname == copyform.formname)) { throw new exception("form name exists!"); } else { var copydata = (from log in db.qmsevaluationforms log.evaluationformid == copyform.copyfrom && log.isactive == true select log).firstordefault(); //insert new form list<qmsevaluationformcontrol> cloneformcontrollist = (from log1 in copydata.qmsevaluationformcontrols log1.isactive==true select log1).tolist(); list<qmsevaluationparameter> clonepaameterlist = (from log1 in copydata.qmsevaluationparameters log1.isactive == true select log1).tolist(); qmsevaluationform newform = new qmsevaluationform(); newform.formname = copyform.formname; newform.makescorezeroonfatal = copydata.makescorezeroonfatal; newform.isactive = true; newform.createdon = datetime.now; newform.subprocessid = copyform.subprocessid; foreach(var control in cloneformcontrollist){ qmsevaluationformcontrol controldata=new qmsevaluationformcontrol(); controldata.caption=control.caption; controldata.createdon=datetime.now; controldata.fieldtypeid=control.fieldtypeid; controldata.formvalidationparameterid=control.formvalidationparameterid; controldata.listnameid=control.listnameid; controldata.ismandatory=control.ismandatory; controldata.maxsize=control.maxsize; controldata.isactive=control.isactive; newform.qmsevaluationformcontrols.add(controldata); } foreach (var parameter in clonepaameterlist) { qmsevaluationparameter parameterdata = new qmsevaluationparameter(); parameterdata.category = parameter.category; parameterdata.parameteercode = parameter.parameteercode; parameterdata.evaluationparameter = parameter.evaluationparameter; parameterdata.listnameid = parameter.listnameid; parameterdata.criticaltypeid = parameter.criticaltypeid; parameterdata.weightage = parameter.weightage; parameterdata.isactive = parameter.isactive; parameterdata.createdon = datetime.now; newform.qmsevaluationparameters.add(parameterdata); } db.qmsevaluationforms.add(newform); db.savechanges(); return new formentity { formid = newform.evaluationformid, formname = newform.formname, makezero = newform.makescorezeroonfatal }; } } }
Comments
Post a Comment