Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

getting array of ID fields in many to many relationship in Entity framework

$
0
0

Hi,

I have in the database table Deal having DealId and table Product having ProductId. I have table ProductDeal having ProductId and DealId for the many to many relation between the tables product and deal. Now in the models I have 

   [Table("Deal")]
    public class Deal
    {
        [Key]
        public int DealId { get; set; }
        public string Title { get; set; }
        public Dealproduct[] DealProducts { get; set; }

    }



    public class Dealproduct
    {
        public int ProductId { get; set; }
    }

 [Table("Product")]
    public class Product
    {
        [Key]
        public int ProductID { get; set; }
        public string ProductTitle { get; set; }
    }

Now in the Context I have 

        public DbSet<Product> Prioducts { get; set; }
        public DbSet<Deal> Deals { get; set; }
        public DbSet<ProductDeal> ProductDeals { get; set; }

when I'm loading the deals:

            var deals = new List<Deal>();
            try
            {
                using (var context = new myContext(connString))
                {
                    var query = context.Deals;
                    deals = query.ToList();
                }

            }
            catch (Exception exc)
            {}

but in this way I'm not getting the list of products for a deal because it is saved in the productDeal table. How do I load the list of products for a deal? I need only list of Ids, not complete list of products.

Thanks


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>