Skip to content
Advertisement

In c# app, how to connect foreign key of subtask table in SQL?

I am trying to create a Task Management System using C# Web API & SQL Server.

Here is what I am trying to achieve:

  • Users can send POST requests to create a Task (with optional sub-tasks).
  • Users can send GET requests to retrieve a Task (displaying all sub-tasks in the response if they exist)

Here are some of my current files:

Task DTO:

Task.cs:

Subtask.cs:

With the above code, I’m currently, able to POST & GET a Task, but I am struggling to add the Sub-Task part.

Can someone please advise what I need to do in order to be able to meet this requirement?

Thanks a lot in advance

Advertisement

Answer

Your classes would link up like this…

You can see that Task has a collection of SubTask and SubTask has a reference back to Task with a Task property and a TaskId.

Update: Since you’re not using EF code-first, as I initially thought.

Your DTO could just have a list of SubTask, something like this:

Then, you could do something like:

This is all hand written, but it’ll be along these lines.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement