MVC中的foreach循环内自动生成新的Guid

MVC中的foreach循环内自动生成新的Guid

本文介绍了如何在Controller和Asp.Net MVC中的foreach循环内自动生成新的Guid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Experts,

我是asp.net mvc的新手,需要你的帮助,如何在foreach循环中自动生成新的Guid,我正在使用List< Expense>在视图模型和GUID作为PrimaryKey,我如何从数据表插入多行到数据库插入。

这里是我的视图模型。我可以插入单行数据,但如果我插入多行数据,

它会显示错误:

违反PRIMARY KEY约束'PK__Expense__EBE72675160F4887'。无法在对象'dbo.Expense'.\r中插入重复键该声明已被终止。。

请帮我急需..

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 ..

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; }



















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 } };
       }

推荐答案

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;
              }


这篇关于如何在Controller和Asp.Net MVC中的foreach循环内自动生成新的Guid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 09:09