Click here to Skip to main content
15,892,281 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello Experts,
I am new to asp.net mvc, need to your help, How can I Automatically Genereate new Guid inside of foreach loop,i am using List<Expense> in view model and GUID as PrimaryKey , how i insert a multiple row from datatable to database insertion.
here is my view model.I can insert a Single Row Data,But if i insert more than one row data,
It will Show the Error Like :
"Violation of PRIMARY KEY constraint 'PK__Expense__EBE72675160F4887'. Cannot insert duplicate key in object 'dbo.Expense'.\r\nThe statement has been terminated.".
Kindly Help Me Need Urgent ..
C#
public System.Guid MaintenanceId { get; set; }
public System.Guid ExpenseSummaryId { get; set; }
public string BillNo { get; set; }
public Nullable<System.DateTime> BillDate { get; set; }
public string VechicleNo { get; set; }
public string ServiceType { get; set; }
public Nullable<double> Amount { get; set; }
public string Reason { get; set; }
public Nullable<System.Guid> UserRightsId { get; set; }
public Nullable<System.Guid> EmployeeId { get; set; }
public string ProviderName { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string ProviderCity { get; set; }
public string ProviderState { get; set; }
public string ProviderCountry { get; set; }
public string Pincode { get; set; }
public int Sno { get; set; }
public Nullable<bool> IsActive { get; set; }
public Nullable<bool> IsDeleted { get; set; }
public Nullable<System.DateTime> CreatedDate { get; set; }
public Nullable<System.DateTime> EditedDate { get; set; }
public Nullable<System.DateTime> LastActiveOn { get; set; }
public Nullable<System.Guid> RowID { get; set; }
public Nullable<System.Guid> CreatedSessionID { get; set; }
public Nullable<System.Guid> EditedSessionID { get; set; }
public Nullable<bool> OfflineMode { get; set; }
public Nullable<System.Guid> OfflineID { get; set; }

public List<Expense> Expenses { get; set; }
public virtual Employee Employee { get; set; }
public virtual UserRight UserRight { get; set; }






C#
public JsonResult SaveOrder(TruckVM TVM)
       {
           bool status = false;
         if (ModelState.IsValid)
           {
            using (TestDBEntities db = new TestDBEntities())
               {
                 var guid = Guid.NewGuid();
                 maintenance ObjMaintenance = new maintenance
                   {
                    MaintenanceId=guid,
                    BillNo=TVM.BillNo,
                    BillDate=TVM.BillDate,
                    Amount=TVM.Amount
                    };

                   foreach (var i in TVM.Expenses)
                   {
                       ObjMaintenance.Expenses.Add(i);

                   }

                   db.maintenances.Add(ObjMaintenance);
                   //db.serviceproviders.Add(ObjServiceProvider);
                   db.SaveChanges();
                   status = true;
               }
           }
           else
           {
               status = false;
           }
           return new JsonResult { Data = new  { status = status } };
       }
Posted
Updated 4-Dec-15 1:21am
v3
Comments
_Asif_ 4-Dec-15 7:20am    
Well you can easily find the issue by just dropping a break point over objMaintenane.Expenses.Add(i) line and debug it, check what insert query is generating to DB. This is a minute job and you can do it

1 solution

Simply move Guid.NewGuid() into for loop instead of adding it once.

C#
using (TestDBEntities db = new TestDBEntities())
              {
                maintenance ObjMaintenance = new maintenance
                  {

                   BillNo=TVM.BillNo,
                   BillDate=TVM.BillDate,
                   Amount=TVM.Amount
                   };

                  foreach (var i in TVM.Expenses)
                  {
                      i.MaintenanceId= Guid.NewGuid();
                      ObjMaintenance.Expenses.Add(i);
                  }

                  db.maintenances.Add(ObjMaintenance);
                  //db.serviceproviders.Add(ObjServiceProvider);
                  db.SaveChanges();
                  status = true;
              }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900