© 2026 Hedgehog Software, LLC

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

❔ Get id from query or DTO

Imagine I have the next two entities

class Task{
public int Id {get;set;}
public string Desc {get;set;}
public int AssignmentId {get;set;}
public Assignment Assignment {get;set;}

class Assignment{
public int Id {get; set;}
public ICollection<Task> Tasks {get;set;}
}

What would be the best way to POST a task to an existing assignment?

How would endpoint and incoming DTO look like?

1) /assignments/3/tasks

Receive
TaskDTO1{
string Desc
}
and assignment id to your controller method,
call service method that will retrieve assignment by the id and append a task to its task collection, then save


2) /tasks/

Receive
TaskDTO2{
string Desc,
int AssignmentId
}
in your controller method, call service method that will create a task entity, retrieve a corresponding assignment by id (AssignmentId from dto2) and fill the Task.Assignment with this assignment, then save


Which way looks better to you? Why?
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

Model, Dto, Entity - Id?
C#CC# / help
2y ago
Converting from DTO to DomainModel
C#CC# / help
2y ago
Empty DTO list from swagger
C#CC# / help
2y ago
❔ FluentValidation DTO
C#CC# / help
3y ago