C
C#

help

❔ Doing post method with entity framework

Ssitua2/2/2023
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
Ccptcrawler2/2/2023
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
Ssitua2/2/2023
one second
Ccptcrawler2/2/2023
take your time 😄
Ssitua2/2/2023
Ssitua2/2/2023
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
Ccptcrawler2/2/2023
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
Ssitua2/2/2023
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?
Ccptcrawler2/2/2023
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
Ssitua2/2/2023
because the "tipo_usuario" is empty and i has to be that way if i want to add a "persona" with the relation
Ccptcrawler2/2/2023
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
Ssitua2/2/2023
from the property class of the DTO i supose
Ccptcrawler2/2/2023
exactly so the property will remain default ( 0 ) and it wont be considered by Entity Framework
Ssitua2/2/2023
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?
Ccptcrawler2/2/2023
yes missing step
Ssitua2/2/2023
when i type "a" , i really mean "i"
Ccptcrawler2/2/2023
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 )
Ssitua2/2/2023
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
Ccptcrawler2/2/2023
you are welcome 😄
AAccord2/3/2023
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.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
❔ I can't reference a class library please helpI can't reference a class library i t says compilation error❔ ASP.NET REMOTE DEVELOPMENTHey does anyone know if I would need nginx for remote development on a ubuntu server from my windowException/NULL/Error handling advice.DeserializeAsync clearly returns a nullable generic object. From the picture we can clearly see that❔ ASP.NET Core web app vs ASP.net web application(.NET Framework)Hi, Ive been working with c# for a while now and would like to make a webapp but I dont know where t✅ IGNORE PEOPLEmy place for testing commands❔ Game Hubhi, how can i program my own game hub, like battle.net where i can download and start my own games ?❔ ✅ beginner class and propertiesi am doing basic program abt class and getting information from input but on the weight section whe❔ Best practice to retrieve MySQL columnsHello wonderful people! Can you please help me understanding what would be the best practice to ret❔ Auto Slide Showhere my code✅ materialDesign:Cardfor some reason it says that closing matrialcard doesent exist and it shows an error❔ Why does this code give the CS8604 warning?I'm confused why the below code gives the CS8604 warning. As far as I am concerned, I am null checki✅ Having trouble adding a picture to my code.what should the path look like?❔ Producer Consumer ISourceBlock<T>, periodically flushing consumed dataHi, I've the following scenario: I've a camera continuously acquiring frames and a c# app receiving ❔ Load async data and display it (WindowsAppSDK / WinUI3)I am currently developing my first windows app with C#, Windows App SDK and WinUI3. I want to do som❔ Best Approach to Implement Authentication and Authorization in Microservices with ASP.NET CoreHi friends, I'm looking for experience share. I have Microservices architecture project, that uses ❔ Performance of many small requests vs less but bigger requestsI have 2 projects: an API hosted on aws and a desktop application. Currently, I'm fetching around 4k✅ Image from dataurlI have dataurl with image and i need to get it to some reasonable image object in c#, so i can resiz✅ why does the test case fail?This is from a longest palindrome puzzle on LeetCode https://leetcode.com/problems/longest-palindrom❔ MongoDB Update nested elementsMongoDB question, I'm trying to add a property to all of my existing documents. The thing is, my doc❔ Grapple HookSo I just made a grapple script but I have a gun camera not the actual gun so how do I add the Grapp