我想一次性插入所有这些表,调试器中的一切顺利,直到遇到SaveChanges()方法,我得到“System.Data.Entity.Infrastructure.DbUpdateException”错误!
表:
public partial class EmployementRequest
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public EmployementRequest()
{
this.EmployeeCourses = new Collection<EmployeeCours>();
this.EmployeeLanguages = new Collection<EmployeeLanguage>();
this.EmployeeSkills = new Collection<EmployeeSkill>();
}
public System.Guid PKEmploymentRequest { get; set; }
public Nullable<System.Guid> FKPerson { get; set; }
public string Name { get; set; }
public string Surnam { get; set; }
public string Father { get; set; }
public string IDNumber { get; set; }
public string IDCardNumber { get; set; }
public string IDCity { get; set; }
public string Birthday { get; set; }
public string Birthplace { get; set; }
public string Nationality { get; set; }
public string Religion { get; set; }
public string Phone { get; set; }
public string Cell { get; set; }
public string Address { get; set; }
public string EmergencyName { get; set; }
public string EmergencyPhone { get; set; }
public string EmergencyAddress { get; set; }
public string ParentedPeople { get; set; }
public string Gender { get; set; }
public string MarriageStatus { get; set; }
public string Residency { get; set; }
public string InsuranceStatus { get; set; }
public string InsuranceCode { get; set; }
public string VehicleStatus { get; set; }
public string VehicleType { get; set; }
public string MilitaryServiceStatus { get; set; }
public string EducationGrade { get; set; }
public string EducatedFrom { get; set; }
public string EducationField { get; set; }
public string Illness { get; set; }
public Nullable<bool> ExtraWorkCapability { get; set; }
public string LeisureTimeHobbies { get; set; }
public Nullable<decimal> Salary { get; set; }
public string IntroductionMethod { get; set; }
public string IntroducerName { get; set; }
public string CreatedOn { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual Collection<EmployeeCours> EmployeeCourses { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual Collection<EmployeeLanguage> EmployeeLanguages { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual Collection<EmployeeSkill> EmployeeSkills { get; set; }
}
public partial class EmployeeCours
{
public System.Guid PKEmlpoyeeCourse { get; set; }
public Nullable<System.Guid> FKEmployementRequest { get; set; }
public string Course { get; set; }
public string Description { get; set; }
public Nullable<int> Duration { get; set; }
public string Date { get; set; }
public string Association { get; set; }
public virtual EmployementRequest EmployementRequest { get; set; }
}
public partial class EmployeeLanguage
{
public System.Guid PKEmployeeLanguage { get; set; }
public Nullable<System.Guid> FKEmploymentRequest { get; set; }
public string Name { get; set; }
public string ComprehensionLevel { get; set; }
public string WritingLevel { get; set; }
public string ConversationLevel { get; set; }
public virtual EmployementRequest EmployementRequest { get; set; }
}
public partial class EmployeeSkill
{
public System.Guid PKEmployeeSkill { get; set; }
public Nullable<System.Guid> FKEmploymentRequest { get; set; }
public string Skill { get; set; }
public string Description { get; set; }
public virtual EmployementRequest EmployementRequest { get; set; }
}
JQuery AJAX函数从表单中检索插入的数据:
function addEmployementRequest() {
var emp = {};
var el = {};
var es = {};
var ec = {};
emp.name = $("#inputName").val();
emp.surnam = $('#inputSurname').val();
emp.father = $('#inputFather').val();
emp.idNumber = $('#inputIDNumber').val();
emp.idCardNumber = $('#inputIDCardNumber').val();
emp.idCity = $('#inputIDCity').val();
emp.birthday = $('#inputBirthday').val();
emp.birthplace = $('#inputBirthplace').val();
emp.Nationality = $('#inputNationality').val();
emp.Religion = $('#inputReligion').val();
emp.Phone = $('#inputPhone').val();
emp.Cell = $('#inputCell').val();
emp.Address = $('#inputAddress').val();
emp.emergencyName = $('#inputEmergencyName').val();
emp.emergencyPhone = $('#inputEmergencyPhone').val();
emp.emergencyAddress = $('#inputEmergencyAddress').val();
emp.parentedPeople = $('#inputParentedPeople').val();
emp.gender = $("input[name='radioGender']:checked").val();
emp.marriageStatus = $('#inputParentedPeople').val();
emp.Residency = $('#selectResidenceStatus option:selected').val();
emp.insuranceStatus = $('#radioInsurance:checked').val();
emp.insuranceCode = $('#inputInsuranceCode').val();
emp.vehicleType = $('#selectVehicleType option:selected').text();
emp.educationGrade = $('#selectEducationGrade option:selected').text();
emp.educatedFrom = $('#inputEducatedFrom').val();
emp.educationField = $('#inputEducationField').val();
emp.illness = $('#inputllness').val();
emp.ExtraWorkCapability = $('#checkboxExtraWorkCapability').is(':checked');
emp.leisureTimeHobbies = $('#inputLeisureTimeHobbies').val();
emp.salary = $('#inputSalary').val();
emp.introductionMethod = $('#inputIntroductionMethod').val();
emp.introducerName = $('#inputIntroducerName').val();
var today = new Date();
emp.createdOn = today.toLocaleDateString("Eng-US");
emp.startDate = $('#startDate').val();
emp.endDate = $('#endDate').val();
emp.position = $('#position').val();
emp.prevSalary = $('#prevsalary').val();
emp.separationReason = $('#separationReason').val();
el.Name = $('#langName').val(),
el.ComprehensionLevel = $('#comprehensionLevel').val(),
el.WritingLevel = $('#writingLevel').val(),
el.ConversationLevel = $('#conversationLevel').val();
ec.Date = $('#courseDate').val(),
ec.Course = $('#course').val(),
ec.Duration = $('#duration').val(),
ec.Association = $('#association').val(),
ec.Description = $('#courseDescription').val();
es.Skill = $('#skill').val(),
es.skillDescription = $('#skillDescription').val();
//emp.EmployeeCourses = [
//{
// Course: $('#course').val(),
// Description: $('#courseDescription'),
// Duration: $('#duration').val(),
// Date: $('#courseDate').val(),
// Association: $('#association').val
//}
//];
//emp.EmployeeSkills = [
//{
// skill: $('#skill').val(),
// skillDescription:$('#skillDescription').val()
//}];
$.ajax({
url: 'EmployementRequestPage.asmx/AddEmployementRequest',
method: 'post',
data: '{emp:' + JSON.stringify(emp) + ',el:' + JSON.stringify(el) + ',es:' + JSON.stringify(es) + ',ec:' + JSON.stringify(ec) + '}',
contentType: "application/json; charset=utf-8",
success: function () {
alert("Success!");
},
error: function (err) {
alert(err);
}
});
};
Mehod
[WebMethod]
public void AddEmployementRequest(EmployementRequest emp,EmployeeLanguage el,EmployeeSkill es,EmployeeCours ec)
{
emp.PKEmploymentRequest = Guid.NewGuid();
EmployeeSkill employeeSkill = new EmployeeSkill
{
PKEmployeeSkill = new Guid(),
FKEmploymentRequest = emp.PKEmploymentRequest,
Skill = es.Skill,
Description = es.Description
};
EmployeeLanguage employeeLanguage = new EmployeeLanguage
{
FKEmploymentRequest = emp.PKEmploymentRequest,
PKEmployeeLanguage = Guid.NewGuid(),
ComprehensionLevel = el.ComprehensionLevel,
ConversationLevel = el.ConversationLevel,
WritingLevel = el.WritingLevel,
Name = el.Name
};
EmployeeCours employeeCours = new EmployeeCours
{
PKEmlpoyeeCourse = Guid.NewGuid(),
FKEmployementRequest = emp.PKEmploymentRequest,
Date = ec.Date ,
Course = ec.Course,
Duration = ec.Duration,
Association = ec.Association,
Description = ec.Description
};
EmployementRequest employementRequest = new EmployementRequest
{
Name = emp.Name,
Address = emp.Address,
Surnam = emp.Surnam,
Father = emp.Father,
IDNumber = emp.IDNumber,
IDCardNumber = emp.IDNumber,
IDCity = emp.IDCity,
Birthday = emp.Birthday,
Birthplace = emp.Birthplace,
Nationality = emp.Nationality,
Religion = emp.Religion,
Phone = emp.Phone,
Cell = emp.Cell,
EmergencyAddress = emp.EmergencyAddress,
EmergencyName = emp.EmergencyName,
EmergencyPhone = emp.EmergencyPhone,
ParentedPeople = emp.ParentedPeople,
Gender = emp.Gender,
MarriageStatus = emp.MarriageStatus,
Residency = emp.Residency,
InsuranceCode = emp.InsuranceCode,
InsuranceStatus = emp.InsuranceStatus,
VehicleType = emp.VehicleType,
MilitaryServiceStatus = emp.MilitaryServiceStatus,
EducatedFrom = emp.EducatedFrom,
EducationField = emp.EducationField,
EducationGrade = emp.EducationGrade,
ExtraWorkCapability = emp.ExtraWorkCapability,
LeisureTimeHobbies = emp.LeisureTimeHobbies,
Salary = emp.Salary,
IntroducerName = emp.IntroducerName,
IntroductionMethod = emp.IntroductionMethod,
Illness = emp.Illness,
VehicleStatus = emp.VehicleStatus,
};
employementRequest.EmployeeLanguages.Add(employeeLanguage);
employementRequest.EmployeeSkills.Add(employeeSkill);
employementRequest.EmployeeCourses.Add(employeeCours);
using (var db = new UKN_DBNAMEEntities())
{
db.EmployementRequests.Add(employementRequest);
db.SaveChanges();
}
}
}
答案 0 :(得分:0)
尝试将代码包含在具有Update Exception或DbValidationException ex
的try catch中:
try
{
using (var db = new UKN_DBNAMEEntities())
{
db.EmployementRequests.Add(employementRequest);
db.SaveChanges();
}
}
catch(DbEntityValidationException ex)
{
//get exception and debug
}
catch(UpdateException ex)
{
//get exception and debug
}