다음보기 모델에 바인딩 된 ASP.Net MVC 폼이 있습니다. 이 양식을 통해 한 제출자는 그의 전화 번호와 약속 유형과 함께 새로운 직원 기록을 삽입 할 수 있습니다. LINQ to SQL을 사용하는 컨트롤러 [HttpPost] 작업은 EContactInfo 및 AppointmentType 테이블을 제외한 People (직원) 테이블 과 연결된 테이블에 단일 레코드 삽입을 수행 할 수 있습니다. _StaffSerivce.Add()는 단순히 데이터 저장소에서 해당 엔티티의 InsertOnSubmit()을 실행합니다. 모든 새 레코드가 추가 된 후 데이터 저장소의 SubmitChanges()가 실행되는 _Service.Save()가 호출됩니다. 그 때 "이미 사용중인 키가있는 엔티티를 추가 할 수 없습니다."이라는 오류가 나타납니다. 첫 번째 전화 번호 또는 약속 유형을 삽입 할 때 오류가 발생하면 첫 번째 레코드가 각 테이블에 삽입 된 후 해당 테이블의 기본 키가 변경되지 않습니다. 일괄 적으로 많은 레코드를 하나의 테이블에 삽입 할 수 있습니까? 감사.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();
}
}
}
오류는 Entity Framework에서 가져온 것이므로이 태그를 추가했습니다. _service.Save 메소드에 대한 코드를 추가하십시오. 오류가 스택에서 발생하는 곳입니다 (가정합니다). – kingdango
의견을 주셔서 감사합니다. EF가 아닌 SQL에 LINQ를 사용하고 있습니다. 원래 게시물에 save() 메소드를 추가했습니다. – user266909
사과드립니다. 나는 총을 뛰어 넘었습니다. :-) – kingdango