Bastieww
Bastieww
CC#
Created by Bastieww on 3/25/2024 in #help
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.
9 replies
CC#
Created by Bastieww on 3/19/2024 in #help
Creating 2 one-to-one relationships with a composite foreign key, EF Code First
Hello everyone, I'm trying to make a database with 2 one-to-one relationships with a composite foreign key,, with this kind of structure : ProductWithColor (that has an Id), Size (that has an Id) and Stock (which has not Id, but a composite Key composed by ProductWithColorId and SizeId). This is code-first, so I'm trying to generate my database with this code I tried everything I found on the Internet, but I can't achieve this. Can anyone help ? Thanks !!
6 replies