【发布时间】:2019-05-30 23:47:49
【问题描述】:
这是我的场景:
我有Company的模型:
public class Company
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual List<Product> Products { get; set; }
public virtual List<Employee> Employees { get; set; }
}
以及如下所示的 API 端点:
//GET: api/company/Profile?id=stringid
[Route("Profile")]
[ResponseType(typeof(Company))]
public IHttpActionResult<Company> GetEmployeeCompany(string userId)
{
var company = db.Companies
.Include(p => p.Products)
.Where(u => u.Employees.userId == userId);
return Ok(company)
//that return dont work ofc, but i just want to show what im talking about
}
我的问题是,如何使用他的产品找到Company,该员工是受雇的?员工具有唯一的 userId,即字符串。我在 api 调用中通过端点传递该字符串
编辑
Employee类
public class Employee
{
[Key]
[ForeignKey("User"), DatabaseGenerated(DatabaseGeneratedOption.None)]
public string UserId { get; set; }
public int CompanyId { get; set; }
public virtual Company Company { get; set; }
public virtual ApplicationUser User { get; set; }
}
【问题讨论】:
标签: c# asp.net entity-framework linq asp.net-web-api