entity-frameworklinq-to-entitiesado.net-entity-data-modelentity-sql

Entity Sql for a Many to Many relationship


Consider two tables Bill and Product with a many to many relationship. How do you get all the bills for a particular product using Entity Sql?


Solution

  • You need to use some linq like this;

    ...
    using (YourEntities ye = new YourEntities())
    {
       Product myProduct = ye.Product.First(p => p.ProductId = idParameter);
       var bills = myProduct.Bill.Load();       
    }
    ...
    

    This assumes that you have used the entitiy framework to build a model for you data. The bills variable will hold a collection of Bill objects that are related to your product object.

    Hope it helps.