Home > Software engineering >  Many to one relationship not shown on find and toList in Linq to entities code first
Many to one relationship not shown on find and toList in Linq to entities code first

Time:08-04

hello im trying to build a rest api with many to one relationship using code first. here is my two entities :

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

namespace RestApiAndForm.Models
{
    public class Categorie
    {
        [Key]
        public int id { get; set; }
        [Required,MaxLength(60)]
        public string nom { get; set; }
        public ICollection<Produit> produits { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace RestApiAndForm.Models
{
    public class Produit
    {
        
        [Key]
        public int id { get; set; }
        [Required,Index,MaxLength(60)]
        public string libelle { get; set; }
        public uint stock;
        [Required]
        public Categorie categorie { get; set; }
    }
}

here is the context :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace RestApiAndForm.Models
{
    public class bdTestContext : DbContext
    {
        public bdTestContext() : base("connTest") { }
        public DbSet<Categorie> categories { get; set; }
        public DbSet<Produit> produits { get; set; }
    }
}

in my controller im using this code :

using RestApiAndForm.Models;
using RestApiAndForm.Models.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace RestApiAndForm.Controllers
{
    public class ProduitController : ApiController
    {
        private  bdTestContext bd = new bdTestContext();
        [HttpGet]
        public List<Produit> GetProduits()
        {
            try
            {
                List<ProduitResponse> produitResponses = new List<ProduitResponse>();
                List<Produit> produits = bd.produits.ToList();
                return produits;
            }
            catch (Exception e)
            {
                return null;
            }
        }

i want to get the product and there categorie but the entity does not come with the categorie information even when im using produit.Find(id) , i cant have access to the categorie entity. this is the result on postman (as you can see all categorie are set to null ) Postman result of the getProduits method does anyone have the solution for that thank you

CodePudding user response:

You should include them like the following :

    List<Produit> produits = bd.produits.Include(p=> p.categorie).ToList();

Refer to : Eager loading

And don't forget to add using System.Data.Entity; to your usings.

  • Related