C
C#9mo ago
Bastieww

Please help a probably really dumb guy (API)

Hey everyone, I'm currently doing an API and I'm pretty sure I'm doing something bad : I have MANY classes, that I designed to be use as an API and for code-first usage (school project, we had no choice). For example, here is the Couleur class :
c#
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace SAES4A01.Models.EntityFramework
{
[Table("t_e_couleur_cou")]
public class Couleur
{
[Key]
[Column("cou_id")]
public int CouleurId { get; set; }

[Column("cou_nom")]
[StringLength(100)]
public string? NomCouleur { get; set; }

[Column("cou_hexa")]
[StringLength(6)]
public string? HexaCouleur { get; set; }



[InverseProperty(nameof(VarianteCouleurProduit.CouleurVarianteCouleurProduit))]
public ICollection<VarianteCouleurProduit> VariantesCouleurProduitCouleur { get; set; }

}
}
c#
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace SAES4A01.Models.EntityFramework
{
[Table("t_e_couleur_cou")]
public class Couleur
{
[Key]
[Column("cou_id")]
public int CouleurId { get; set; }

[Column("cou_nom")]
[StringLength(100)]
public string? NomCouleur { get; set; }

[Column("cou_hexa")]
[StringLength(6)]
public string? HexaCouleur { get; set; }



[InverseProperty(nameof(VarianteCouleurProduit.CouleurVarianteCouleurProduit))]
public ICollection<VarianteCouleurProduit> VariantesCouleurProduitCouleur { get; set; }

}
}
It's one of the tiniest classes among the project. In order to post a new couleur, I would like to just pass the following json :
{
"couleurId": 105,
"nomCouleur": "Discord Color",
"hexaCouleur": "#454545"
}
{
"couleurId": 105,
"nomCouleur": "Discord Color",
"hexaCouleur": "#454545"
}
BUT : when i try to do so, i get this error :
"errors": {
"VariantesCouleurProduitCouleur": [
"The VariantesCouleurProduitCouleur field is required."
]
}
"errors": {
"VariantesCouleurProduitCouleur": [
"The VariantesCouleurProduitCouleur field is required."
]
}
So I added the VariantesCouleurProduitCouleur, which is the navigation property between Couleur and VariantesCouleurProduit ;
{
"couleurId": 105,
"nomCouleur": "string",
"hexaCouleur": "string",
"variantesCouleurProduitCouleur": []
}
{
"couleurId": 105,
"nomCouleur": "string",
"hexaCouleur": "string",
"variantesCouleurProduitCouleur": []
}
and that worked.
5 Replies
Bastieww
BastiewwOP9mo ago
As this class is clearly the simpliest one, I'm afraid I'll need to set a null property for EACH navigation property included in EACH class. Do you know if that's a classic behavior, or did I full mistaken my code in the whole project ? Ty for your answer !
Jimmacle
Jimmacle9mo ago
your EF models shouldn't be anywhere near JSON the database model is for the database only, you should have different models for your API requests and responses then you map the API models to/from the database models in code otherwise you get issues like this where the database model has different properties than you want in your API
Bastieww
BastiewwOP9mo ago
I'm not sure to understand, what changes should I make exactly ? Because the database is generated in code-first, so by using API's models. Here again, I'm surely misunderstanding how the whole thing works.
Jimmacle
Jimmacle9mo ago
you have API request -> DB model -> database you should have API request -> API model -> DB model -> database
Bastieww
BastiewwOP9mo ago
ok, I'll try to do that, ty
Want results from more Discord servers?
Add your server