✅ ASP.NET API (EF Core) Relationships

Using the following example in my learnings, I'm struggling to make this work the way I want it to.
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Employee> Employees { get; set; }
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public Company Company { get; set; }
}
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Employee> Employees { get; set; }
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public Company Company { get; set; }
}
When using swagger to POST a new Employee, if I want to specify a related Company I have to Include all the fields from the Company table, and the API creates a new Company record along with the new Employee record. So this is what it requires right now:
{
"name": "New Company",
"phone": "(123) 456 789",
"company": {
"name": "New Company"
}
}
{
"name": "New Company",
"phone": "(123) 456 789",
"company": {
"name": "New Company"
}
}
What I'd like to do is provide just a Company.Id for an existing Company record... So something like the this to add a new Employee, which includes the id 100 of an existing Company record:
{
"name": "New Company",
"phone": "(123) 456 789"
"company": 100
}
{
"name": "New Company",
"phone": "(123) 456 789"
"company": 100
}
I'm not sure how to make that work?
2 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.