Bonjour à tous,

En vue d'un petit projet je m'exerce avec un petit exemple de connexion à une base de données et j'essaye de remplir cette base avec une fonction pour l'exercice.
Si je remplis une table tout cela se passe bien - c'est lorsque je remplis la deuxième table que cela coince.
Table 1 : Produits (un produit peut appartenir à une catégorie)
Table 2 : Categories (une catégorie peut être définie pour plusieurs produits)

Dans mon projet j'ai deux models pour définir ces tables :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
namespace CH72_EvaluationProduitMVC.Datas
{
    public class Produit
    {
        [Key]
        public string ProduitID { get; set; }
        public string ProduitNom { get; set; }
        public string ProduitDescription { get; set; }
        public DateTime ProduitDateDeCreation { get; set; }
        public int ProduitMoyenneEvaluation { get; set; }
        public decimal ProduitPrix { get; set; }
        [ForeignKey("CategorieID ")]
        public Categorie Categorie { get; set; }
        [Required]
        [Display(Name = "Categorie")]
        public string ProduitCategorieID { get; set; }
    }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
namespace CH72_EvaluationProduitMVC.Datas
{
    public class Categorie
    {
        [Key]
        public string CategorieID { get; set; }
        public string CategorieNom { get; set; }
        public string CategorieDescription { get; set; }
        public DateTime CategorieDateDeCreation { get; set; }
 
        public ICollection<Produit> Produits { get; set; }
    }
}
Ainsi qu'une classe DBContext
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
namespace CH72_EvaluationProduitMVC.Datas
{
    public class ProduitContext : DbContext
    {
        public ProduitContext(DbContextOptions<ProduitContext> options) : base(options)
        {
 
        }
 
        public DbSet<Produit> Produits { get; set; }
        public DbSet<Categorie> Categories { get; set; }
}
Voici la fonction qui se charge de remplir la base de données :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 private void ChargerDonnees()
{
            var rnd = new Random();
            for (int i = 0; i < 40; i++)
            {
                var produit = new Produit
                {
                    ProduitID = i.ToString(),
                    ProduitNom = $"Produit :{i}",
                    ProduitDescription = $"C'est la description du produit numero:{i}",
                    ProduitDateDeCreation = DateTime.Now,
                    ProduitMoyenneEvaluation = 0,
                    ProduitPrix = rnd.Next(),
                    ProduitCategorieID = new Categorie
                    {
                        CategorieID = Guid.NewGuid().ToString(),
                        CategorieDescription = $"C'est la description de la catégorie numero:{i}",
                    }.CategorieID
                };
                _produitContext.Produits.Add(produit);
                _produitContext.SaveChanges();
            }
}
En faisant ceci j'obtiens une erreur : SqlException*: Invalid column name 'CategorieID'.

Quelqu'un pourrait il m'éclairer ? Un grand merci d'avance