© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
4 replies
dreamdoctor

✅ 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
Employee
, if I want to specify a related
Company
Company
I have to Include all the fields from the Company table, and the API creates a new
Company
Company
record along with the new
Employee
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
Company.Id
for an existing
Company
Company
record... So something like the this to add a new
Employee
Employee
, which includes the id
100
100
of an existing
Company
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?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Polymorphic relationships in EF Core / .NET Core
C#CC# / help
3y ago
ASP.NET Core Blazor + EF error
C#CC# / help
3y ago
ASP.NET CORE MVC VS ASP.NET CORE WEB API
C#CC# / help
17mo ago
✅ ASP.Net Core Web API
C#CC# / help
6mo ago