public bool EmployeeListAdd(List <EmployeeAddExcle> empList, string createPerson) { using (SysEntities db = new SysEntities()) { using (var tran = db.Database.BeginTransaction()) { try { if (empList != null) { foreach (EmployeeAddExcle employ in empList) { Employee employee = new Employee(); //员工表 EmployeeContact employeeContact = new EmployeeContact(); //联系人表 CompanyEmployeeRelation companyEmployeeRelation = new CompanyEmployeeRelation(); //员工企业关系表 EmployeeBank employeebank = new EmployeeBank(); //员工银行 //员工基本 employee.Name = employ.Empname; employee.CertificateType = employ.CertificateType; employee.CertificateNumber = employ.CertificateNumber; employee.AccountType = employ.AccountType; employee.Sex = Common.CardCommon.Getsex(employ.CertificateNumber) == 1 ? "男" : "女"; employee.Birthday = Common.CardCommon.GetShengRi(employ.CertificateNumber); employee.CreateTime = DateTime.Now; employee.CreatePerson = createPerson; db.Employee.Add(employee); //联系人 employeeContact.Telephone = employ.Telephone; employeeContact.MobilePhone = employ.MobilePhone; employeeContact.Address = employ.Address; employeeContact.Email = employ.Email; employeeContact.EmployeeId = employee.Id; employeeContact.State = "启用"; employeeContact.CreateTime = DateTime.Now; employeeContact.CreatePerson = createPerson; db.EmployeeContact.Add(employeeContact); db.SaveChanges(); // 银行信息 //if (!string.IsNullOrWhiteSpace(employeebank.AccountName)) { employeebank.EmployeeId = employee.Id; employeebank.AccountName = employ.AccountName; employeebank.Bank = employ.Bank; employeebank.BranchBank = employ.BranchBank; employeebank.Account = employ.Account; employeebank.IsDefault = employ.Account; employeebank.State = "启用"; employeebank.CreateTime = DateTime.Now; employeebank.CreatePerson = createPerson; db.EmployeeBank.Add(employeebank); db.SaveChanges(); } //员工关系表 companyEmployeeRelation.CityId = employ.City; companyEmployeeRelation.CompanyId = employ.CompanyId; companyEmployeeRelation.EmployeeId = employee.Id; companyEmployeeRelation.State = "在职"; companyEmployeeRelation.Station = employ.Station; companyEmployeeRelation.PoliceAccountNatureId = employ.PoliceAccountNature; companyEmployeeRelation.CreateTime = DateTime.Now; companyEmployeeRelation.CreatePerson = createPerson; db.CompanyEmployeeRelation.Add(companyEmployeeRelation); db.SaveChanges(); } } tran.Commit(); return(true); } //catch //{ // tran.Rollback(); // return false; //} catch (System.Data.Entity.Validation.DbEntityValidationException ex) { tran.Rollback(); foreach (var errors in ex.EntityValidationErrors) { foreach (var item in errors.ValidationErrors) { Console.WriteLine(item.ErrorMessage + item.PropertyName); } } return(false); } } } }
/// <summary> /// 添加员工 /// </summary> /// <param name="baseModel"></param> /// <param name="contactModel"></param> /// <param name="bankModel"></param> /// <returns></returns> public int EmployeeAdd(Employee baseModel, EmployeeContact contactModel, EmployeeBank bankModel, CompanyEmployeeRelation relationModel) { using (SysEntities db = new SysEntities()) { using (var tran = db.Database.BeginTransaction()) { try { //创建公司 db.Employee.Add(baseModel); db.SaveChanges(); //添加联系方式 contactModel.EmployeeId = baseModel.Id; db.EmployeeContact.Add(contactModel); //添加银行信息 if (bankModel != null) { bankModel.EmployeeId = baseModel.Id; db.EmployeeBank.Add(bankModel); } //企业员工关系 relationModel.EmployeeId = baseModel.Id; db.CompanyEmployeeRelation.Add(relationModel); db.SaveChanges(); tran.Commit(); return(1); } catch (Exception ex) { tran.Rollback(); return(0); } } } }