简体   繁体   English

实体框架导航属性不起作用

[英]Entity Framework Navigation property not working

I have a many to many relationship like below. 我有如下所示的多对多关系。

  • Company[CompanyID, Name] - nav prop SalaryTabs Company [CompanyID,Name]-导航道具SalaryTabs
  • SalaryTab[ID,Salary, Since, CompanyId, Employeeid] - nav prop (Company, Employee), and SalaryTab [ID,Salary,Since,CompanyId,Employeeid]-导航道具(公司,员工)和
  • Employee[EmployeeID,FirstName, LastName, DOB] -nav prop Employee [EmployeeID,FirstName,LastName,DOB] -nav prop
    (SalaryTabs) (SalaryTabs)

But When I try to use Linq to query the tables using the navigation property. 但是,当我尝试使用Linq使用navigation属性查询表时。 It wont just show up in the intellisence at all. 它不会完全显示在智能中。

For example, I want to access contxt.SalaryTabs.Company.xxx The navigation property Company will not load the xxx and will not show up in the intellisence and if I manually type it. 例如,我要访问contxt.SalaryTabs.Company.xxx导航属性Company将不会加载xxx,并且不会显示在智能中,如果我手动键入它。 I get some errors. 我遇到一些错误。

If I try to do something like 如果我尝试做类似的事情

//Delete an employee (identified via id) from a specific company(identified via id)

public bool DeleteEmployeeFromSpecificCompany(Guid employeeID, Guid companyID)
{
    try
    {
        var emp = dbContext.Employees.FirstOrDefault(x => x.EmployeeID == employeeID);
        dbContext.Companies.FirstOrDefault(x => x.CompanyID == companyID).SalaryTabs.Employee.Remove(emp);

        dbContext.SaveChanges();
    }
    catch (Exception)
    {
        return false;
    }
    return true;
}

The navigation property doesn't work. 导航属性无效。 I can't access context.Companies.SalaryTab.xxxx for example. 例如,我无法访问context.Companies.SalaryTab.xxxx。 I have been having this problem since yester which I didn't have before. 从昨天开始我就一直遇到这个问题,这是我以前从未有过的。 I could navigate from one entity to another using the navigation properties but now it won't load and offer the options anymore. 我可以使用导航属性从一个实体导航到另一个实体,但是现在它不再加载并提供选项。

I appreciate any input. 感谢您的投入。

I just can't navigate between entities anymore like before. 我只是再也无法像以前那样在实体之间导航。 I got the employee entity which i want to remove and the navigation wont let me through. 我有要删除的员工实体,导航无法让我通过。 I don't get the option to select the property I want to get to. 我没有选择要访问的属性的选项。 I tried all day yesterday and it's the same until now. 我昨天整天都在尝试,直到现在都一样。

var emp = dbContext.Employees.FirstOrDefault(x => x.EmployeeID == employeeID);

            dbContext.Companies.FirstOrDefault(x => x.CompanyID == companyID).SalaryTabs.Employee.Remove(emp);

            dbContext.SaveChanges();

SalaryTabs - is collection. SalaryTabs-是集合。 So, you need select one: 因此,您需要选择一个:

dbContext.Companies.FirstOrDefault(x => x.CompanyID == companyID).SalaryTabs.FirstOrDefault(...condition...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM