Question

I'm building a master details page with batch editing, that is multiple details records with single master record. but only one detail record is being saved into the data base. I tried to debug & found that detail loop is executing multiple time accurately but not the saving multiple data. Here is my Code for save method:

public ActionResult CMN_VAL_FORM(HRM_CMN_VLU_MST_ViewModel model)
    {
        //var ctx=new Entities1();
        var CMN_VLU_MST_OBJ = new HRM_CMN_VLU_MST();
        var CMN_VLU_DTL_OBJ = new HRM_CMN_VLU_DTL();

        //using (TransactionScope transaction = new TransactionScope())
        //{

            using (var ctx = new Entities1())
            {
                var type_code = ctx.ExecuteStoreQuery<string>("select get_pk_code('hrm_cmn_vlu_mst','CMN_VLU_TYPE_CODE') from dual").SingleOrDefault(); //A scalar function to generate the code in the format yymmdd0001
                var value_code = ctx.ExecuteStoreQuery<string>("select get_pk_code('hrm_cmn_vlu_dtl','CMN_VLU_CODE') from dual").SingleOrDefault();
                CMN_VLU_MST_OBJ.CMN_VLU_TYPE_CODE = type_code;
                CMN_VLU_MST_OBJ.CMN_VLU_REM = model.CMN_VLU_REM;
                CMN_VLU_MST_OBJ.CMN_VLU_TYPE_FOR = model.CMN_VLU_TYPE_FOR;
                CMN_VLU_MST_OBJ.CMN_VLU_TYPE_SRTNM = model.CMN_VLU_TYPE_SRTNM;
                CMN_VLU_MST_OBJ.ENTRY_DATE = DateTime.Now;
                CMN_VLU_MST_OBJ.CMN_VLU_TYPE_NAME = model.CMN_VLU_TYPE_NAME;

                foreach (var item in model.HRM_CMN_VLU_DTL)
                {

                    CMN_VLU_DTL_OBJ.CMN_VLU_LEVL = item.CMN_VAL_LEVL;
                    CMN_VLU_DTL_OBJ.CMN_VLU_REM = item.CMN_VLU_REM;
                    CMN_VLU_DTL_OBJ.CMN_VLU_SLNO = item.CMN_VLU_SLNO;
                    CMN_VLU_DTL_OBJ.CMN_VLU_TITL = item.CMN_VAL_TITL;
                    CMN_VLU_DTL_OBJ.CMN_VLU_CNTN = item.CMN_VAL_CNTN;
                    CMN_VLU_DTL_OBJ.CMN_VLU_CODE = value_code;
                    CMN_VLU_DTL_OBJ.CMN_VLU_TYPE_CODE = type_code;
                    CMN_VLU_DTL_OBJ.ENTRY_DATE = DateTime.Now;
                    CMN_VLU_DTL_OBJ.MAIL_ADDR_INT = item.MAIL_ADDR_INT;
                    CMN_VLU_DTL_OBJ.MAIL_ADDR_EXT = item.MAIL_ADDR_EXT;
                    CMN_VLU_DTL_OBJ.MAIL_AUTO_SEND_INT = item.MAIL_AUTO_SEND_INT;
                    CMN_VLU_DTL_OBJ.MAIL_AUTO_SEND_EXT = item.MAIL_AUTO_SEND_EXT;
                    CMN_VLU_DTL_OBJ.ACTIVE_STATUS = item.ACTIVE_STATUS;
                    CMN_VLU_MST_OBJ.HRM_CMN_VLU_DTL.Add(CMN_VLU_DTL_OBJ);

                    ctx.SaveChanges();
                    var temp_value_code = Int32.Parse(value_code);
                    temp_value_code++;
                    value_code = temp_value_code.ToString();
                    ctx.HRM_CMN_VLU_MST.AddObject(CMN_VLU_MST_OBJ);
                }


                ctx.SaveChanges();

            //    transaction.Complete();

            //}
        }



        return View();
    }

No Error message for the code, but not saving multiple detail records. What I'm doing wrong?

Was it helpful?

Solution

What I was doing wrong, I created the object just once & updated that same object every time while executing the foreach loop! I just moved the object declaration into the foreach loop & it works like a charm!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top