❔ 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
19 Replies
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 implementationone second
take your time 😄
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
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
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?
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
because the "tipo_usuario" is empty and i has to be that way if i want to add a "persona" with the relation
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
and then map that doing something like (you can also use libraries as AutoMapper or Mapster but for little things they have no sense)
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 jsonfrom the property class of the DTO i supose
exactly
so the property will remain default ( 0 ) and it wont be considered by Entity Framework
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?
yes missing step
when i type "a" , i really mean "i"
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 )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
you are welcome 😄
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.