2012-07-01 17 views
0

我有一个ASP.Net MVC窗体绑定到以下视图模型。该表格允许一次提交插入新的工作人员记录以及他的所有电话号码和预约类型。使用LINQ to SQL的控制器[HttpPost]操作可以对与People(aka)表关联的表执行单个记录插入,除了EContactInfo和AppointmentType表以外。 _StaffSerivce.Add()只是在数据仓库中执行该实体的InsertOnSubmit()。在添加完所有新记录后,将调用_Service.Save(),以执行数据存储库的SubmitChanges()。那是当我收到错误说“无法添加一个已使用密钥的实体”。错误是在插入第一个电话号码或约会类型时,在将第一条记录插入相应表格后,这些表格的主键不会更改。如何批量插入多个记录到一个表中?谢谢。ASP.Net LINQ批处理插入记录到表

public class RequestForAppointmentViewModel 
{ 
    public People Staff { get; set; } /* Person to be appointed or employeed */ 
    public Appointment Appointment { get; set; } 
    public ContactAddress PostAddress { get; set; } 

    //public IList<EContactInfo> ContactMethods { get; set; } 
    public string WorkPhone { get; set; } 
    public string HomePhone { get; set; } 
    public string CellPhone { get; set; } 
    public string Pager { get; set; } 
    public string EmailAddress { get; set; } 

    //public IList<AppointmentType> AppointmentTypes { get; set; } 
    public bool ChiefResident { get; set; } 
    public bool CompPen { get; set; } 
    public bool Consultant { get; set; } 
    public bool ContractAppointmentType { get; set; } 
    public bool FeeBasis { get; set; } 
    public bool Locum { get; set; } 
    public bool SpecialFellow { get; set; } 
    public bool StaffAppointmentType { get; set; } 
    public bool StaffFullTime { get; set; } 
    public bool StaffIntermittent { get; set; } 
    public bool StaffPartTime { get; set; } 
    public bool StaffPermanent { get; set; } 
    public bool StaffTemporary { get; set; } 
    public bool WOC { get; set; } 

    public Contract Contract { get; set; } 
    public ContractingCompany ContractingCompany { get; set; } /* person is via contracting company */ 
} 

回发动作有以下

 [HttpPost] 
    public ActionResult CreateRequestForAppointment(RequestForAppointmentViewModel requestForm) 
    { 
     bool success = false; 
     string alpha = string.Empty; 

     if (!ModelState.IsValid) 
      return View("RequestForAppointment", requestForm); 
     else 
     { 
      try 
      { 
       People thisPerson = new People(); 
       if (TryUpdateModel<People>(thisPerson, "Staff")) //prefixed 
       { 
        #region persist People 
        _staffService.Add(thisPerson); 
        alpha = thisPerson.LName.Substring(0, 1); 
        #endregion persist People 

        #region persist Appointment 
        Appointment thisAppointment = new Appointment(); 
        if (TryUpdateModel<Appointment>(thisAppointment, "Appointment")) 
        { 
         thisAppointment.People = thisPerson; 
         _staffService.Add(thisAppointment); 
        } 
        #endregion persist Appointment 

        #region persist ContactAddress 
        ContactAddress thisAddress = new ContactAddress(); 
        if (TryUpdateModel<ContactAddress>(thisAddress, "PostAddress")) 
        { 
         thisAddress.People = thisPerson; 
         thisAddress.Country = _staffService.ListCountries().SingleOrDefault(c => c.CountryName.Equals("USA")); 
         _staffService.Add(thisAddress); 
        } 
        #endregion persist ContactAddress 

        #region persist Contract Company 
        ContractingCompany thisCompany = new ContractingCompany(); 
        if (TryUpdateModel<ContractingCompany>(thisCompany, "ContractingCompany")) 
        { 
         _staffService.Add(thisCompany); 
        } 
        #endregion persist Contract Company 

        #region persist Contract 
        Contract thisContract = new Contract(); 
        if (TryUpdateModel<Contract>(thisContract, "Contract")) 
        { 
         thisContract.Appointment = thisAppointment; 
         thisContract.ContractingCompany = thisCompany; 
         _staffService.Add(thisContract); 
        } 
        #endregion Persist Contract 

        #region persist EContactInfo 
        if (!string.IsNullOrWhiteSpace(requestForm.WorkPhone)) 
        { 
         EContactInfo WorkPhone = new EContactInfo(); 
         WorkPhone.People = thisPerson; 
         WorkPhone.CodeReference = _staffService.ListMessagingMethods().SingleOrDefault(m => m.Description.Equals("Work Phone")); 
         WorkPhone.ContactDetail = requestForm.WorkPhone; 
         _staffService.Add(WorkPhone); 
        } 

        if (!string.IsNullOrWhiteSpace(requestForm.HomePhone)) 
        { 
         EContactInfo HomePhone = new EContactInfo(); 
         HomePhone.People = thisPerson; 
         HomePhone.CodeReference = _staffService.ListMessagingMethods().SingleOrDefault(m => m.Description.Equals("Home Phone")); 
         HomePhone.ContactDetail = requestForm.HomePhone; 
         _staffService.Add(HomePhone); 
        } 

        //others phones are omitted for abbreviation 
        #endregion persist EContactInfo 

        #region persist AppointmentType 
        if (requestForm.ChiefResident.Equals(true)) 
        { 
         AppointmentType ChiefResident = new AppointmentType(); 
         ChiefResident.Appointment = thisAppointment; 
         ChiefResident.CodeReference = _staffService.ListAppointmentTypes().SingleOrDefault(t => t.Description.Equals("Chief Resident (CRES)")); 
         _staffService.Add(ChiefResident); 
        } 
        if (requestForm.CompPen.Equals(true)) 
        { 
         AppointmentType CompPen = new AppointmentType(); 
         CompPen.Appointment = thisAppointment; 
         CompPen.CodeReference = _staffService.ListAppointmentTypes().SingleOrDefault(t => t.Description.Equals("Comp & Pen (C&P)")); 
         _staffService.Add(CompPen); 
        } 
        //Other types omitted for abbreviation 
        #endregion persist AppointmentType 

       _staffService.Save(); 

        success = true; 
       } 
      } 

储存库方法如下。

public class ClinicalPrivilegeRepository : IClinicalPrivilegesRespository 
{ 
    private DB db = new DB(); 

    #region EContactIfo 

    public IQueryable<EContactInfo> GetEContactInfoByPersonId(int id) 
    { 
     var eContactInfos = (from e in db.EContactInfos 
          select e).Where(e => e.FK_People.Equals(id)); 
     return eContactInfos; 
    } 

    public EContactInfo GetEcontactInfoById(int id) 
    { 
     var eContactInfo = (from e in db.EContactInfos 
          select e).SingleOrDefault(e => e.PKey.Equals(id)); 
     return eContactInfo; 
    } 

    public void Add(EContactInfo newEContactInfo) 
    { 
     db.EContactInfos.InsertOnSubmit(newEContactInfo); 
    } 

    public void Delete(EContactInfo thisEContactInfo) 
    { 
     db.EContactInfos.DeleteOnSubmit(thisEContactInfo); 
    } 

    #endregion EContactIfo 

#region StaffAppointmentType 

    public IQueryable<AppointmentType> ListStaffAppointmentTypes(int appointmentId) 
    { 
     IQueryable<AppointmentType> appointmentTypes = (db.AppointmentTypes.Where(t => t.FK_Appointment.Equals(appointmentId)).Select(t => t)); 
     return appointmentTypes; 
    } 

    public AppointmentType GetStaffAppointmentType(int appointmentId, int appointmentTypeId) 
    { 
     var appointmentType = (from t in ListStaffAppointmentTypes(appointmentId) 
            select t).SingleOrDefault(x=>x.FK_AppointmentType.Equals(appointmentTypeId)); 
     return appointmentType; 
    } 

    public void Add(AppointmentType newAppointmentType) 
    { 
     db.AppointmentTypes.InsertOnSubmit(newAppointmentType); 
    } 

    public void Delete(AppointmentType thisAppointmentType) 
    { 
     db.AppointmentTypes.DeleteOnSubmit(thisAppointmentType); 
    } 

    #endregion StaffAppointmentType 

public void Save() 
    { 
     //throw new NotImplementedException(); 
     db.SubmitChanges(); 
    } 
} 

}

+0

你得到的错误是来自实体框架,所以我添加了该标签。请为您的_service.Save方法添加代码,因为这是发生在堆栈中的错误(我假设)。 – kingdango

+0

感谢您的评论。我正在使用LINQ to SQL,而不是EF。我在原始文章中添加了save()方法。 – user266909

+0

我的歉意我跳了枪。 :-) – kingdango

回答

0

的代码大多是正确的。真正的问题出在DBML的EContactInfo和AppointmentType表中,我忘了将主键的标识设置为true。