在Entity Framework中插入子表时无法访问属性

时间:2017-06-14 15:16:27

标签: c# entity-framework linq

enter image description here

    [WebMethod]
    public void AddEmployementRequest(EmployementRequest emp)
    {   
        EmployeeSkill employeeSkill = new EmployeeSkill
        {
            Skill = emp.,
            Description = emp.
        };
        EmployeeLanguage employeeLanguage = new EmployeeLanguage
        {
            Name = emp.,
            ConversationLevel = emp.
        };
        EmployeeCours employeeCours = new EmployeeCours
        {
            Date =emp. ,
            Course = emp.,
            Duration = emp.,
            Association = emp.,
            Description = emp.
        };
        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,
            PKEmploymentRequest = Guid.NewGuid(),
        };
        employementRequest.EmployeeLanguages.Add(employeeLanguage);
        employementRequest.EmployeeSkills.Add(employeeSkill);
        employementRequest.EmployeeCourses.Add(employeeCours);
        using (var db = new UKN_DBNAMEEntities())
        {
            db.EmployementRequests.Add(employementRequest);
            db.SaveChanges();
        }
    }

我想一次性插入所有父表和子表,正如您所看到的,我无法访问子表中的属性,也没有智能感知来显示与父类不同的属性

我想我需要一个Linq查询,但我不知道

1 个答案:

答案 0 :(得分:0)

你试过吗

[WebMethod]
public void AddEmployementRequest(EmployementRequest emp) {           
    emp.PKEmploymentRequest = Guid.NewGuid();
    using (var db = new UKN_DBNAMEEntities()) {
        db.EmployementRequests.Add(emp);
        db.SaveChanges();
    }
}

它可能/应该这样做,但是......

考虑:

  • 使用automapper等;
  • 使用一些查询来避免重新使用语言或Skil