如何修复错误'无法将类型'字符串'隐式转换为'int? in int latestId = employeeDetails.Person_Id;

时间:2018-03-01 15:29:18

标签: c#

namespace HRMS.Models.Service
{
    public class EmployeeService
    {
        public void SaveEmployeeDetails(EmployeeViewModel employee)
        {
            Employee employeeDetails = new Employee();

            employeeDetails.First_Name = employee.First_Name;
            employeeDetails.Father_Name = employee.Father_Name;
            employeeDetails.G_Father_Name = employee.G_Father_Name;
            employeeDetails.Mothers_Full_Name = employee.Mothers_Full_Name;
            employeeDetails.Religion = employee.Religion;
            employeeDetails.Sex = employee.Sex;
            employeeDetails.Birth_Date = employee.Birth_Date;
            employeeDetails.Birth_Place = employee.Birth_Place;
            employeeDetails.Marital_Status = employee.Marital_Status;
            employeeDetails.Nation = employee.Nation;
            employeeDetails.Nationality = employee.Nationality;
            employeeDetails.Medical_Fitness = employee.Medical_Fitness;
            employeeDetails.Registered_Date = employee.Registered_Date;
            employeeDetails.Job_Place = employee.Job_Place;
            employeeDetails.Department = employee.Department;
            employeeDetails.Position = employee.Position;
            employeeDetails.Rank = employee.Rank;
            employeeDetails.Step = employee.Step;
            employeeDetails.Salary = employee.Salary;
            employeeDetails.Status = employee.Status;
            employeeDetails.Remark = employee.Remark;



            using (HRMSEntities employeeContext = new HRMSEntities())
            {
                employeeContext.Employee.Add(employeeDetails);
                employeeContext.SaveChanges();

            }
            int latestId = employeeDetails.Person_Id;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

employeeDetails.Person_Id是一个字符串,您正在尝试将其分配给int变量

您还需要将latestId更改为字符串

string latestId = employeeDetails.Person_Id;

或者您需要转换为int(只要您确定Person_Id实际上包含整数值)

int latestId = Convert.ToInt32(employeeDetails.Person_Id);