C
C#2y ago
situa

❔ Doing post method with entity framework

i'm making a simple API to practice with entity framework but when i try to make a post to a table with relations i have to pass a nested json instead of only the id, it has to be exactly like this json otherwise it will rise and error saying that "persona" field is required. here are my models of "persona" , am i missing something? o making the relation wrong? all documentation or tutorial use a post with a table without relationships so i'm Smadge
19 Replies
cptcrawler
cptcrawler2y ago
the relationships seems good to me, you may want to pass the Persona class as parameter in the POST controller ( would be better if you pass a DTO Data Transfer Object and that convert that back later to the real Persona class If you can share you controller implementation
situa
situaOP2y ago
one second
cptcrawler
cptcrawler2y ago
take your time 😄
situa
situaOP2y ago
situa
situaOP2y ago
this is my controller honestly i just learned about DTO about 2-3 hours ago so maybe that would help me but idk i still think the swagger would ask me that json structure or idk
cptcrawler
cptcrawler2y ago
i mean your implementation is correct a little tip inside the routeAttribute over the controller itself use [Route("[controller]") intead of hard typing it btw i can't actually see what's ur problem you actually don't know how to call the endpoint ? and i don't see a problem in that structure to be honest i mean
situa
situaOP2y ago
i wanted to be sure that the json structure that i'm using is the optimal one because i want to do thinks the right way and where do a specify the name of it?
cptcrawler
cptcrawler2y ago
ooo that's another kind of question that's actually inferring the name from ur controller class name exluding controller so it will be something like
situa
situaOP2y ago
because the "tipo_usuario" is empty and i has to be that way if i want to add a "persona" with the relation
cptcrawler
cptcrawler2y ago
your name PersonaController so [controller] will have Persona the thing you can do is declare a DTO like PersonaUpdateRequest (not the optimal name) having something like
class PersonaUpdateRequest{
public int Id_persona { get; set; }
public string Nombre { get; set; }
// etc without actually inserting the relationship
// auto properties so you won't have any reference
// inside here about Tipo and Genero
class PersonaUpdateRequest{
public int Id_persona { get; set; }
public string Nombre { get; set; }
// etc without actually inserting the relationship
// auto properties so you won't have any reference
// inside here about Tipo and Genero
and then map that doing something like (you can also use libraries as AutoMapper or Mapster but for little things they have no sense)
public static class MyMaps{

public static Persona ConvertToPersona(PersonaUpdateRequest request){
return new Persona(){
Id_persona = request.Id_persona,
// etc...
}
}
public static class MyMaps{

public static Persona ConvertToPersona(PersonaUpdateRequest request){
return new Persona(){
Id_persona = request.Id_persona,
// etc...
}
}
doing this way the resulting Persona will have the auto properties Tipo and Genero to null and you will have to insert the Persona by it self another thing if you don't want to set the Id by hand you may need to remove the Id_persona property from the class so the json won't require it in the post it's a matter of shaping the DTO for shaping the request json
situa
situaOP2y ago
from the property class of the DTO i supose
cptcrawler
cptcrawler2y ago
exactly so the property will remain default ( 0 ) and it wont be considered by Entity Framework
situa
situaOP2y ago
okay okay, i will make a recap to see if i get it : i can use DTO so the json structure that a send doesn't restrict me to certain fields and that way transfer the data to the table of the data base?
cptcrawler
cptcrawler2y ago
yes missing step
situa
situaOP2y ago
when i type "a" , i really mean "i"
cptcrawler
cptcrawler2y ago
you need to convert the PersonaCreateRequest to the entity Persona of your DbContext so when mapping the values all the ones that you don't need will remain as default values ( so null for autoproperties and classes and default for the rest )
situa
situaOP2y ago
okay okay i get it now and i will implement the DTO and test i got a interview tumorrow and i want to do my best thank you so much
cptcrawler
cptcrawler2y ago
you are welcome 😄
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.
Want results from more Discord servers?
Add your server