0

I have a database-first application, with entities Promotion and Product, and the navigation property Promotion.Products. There are other navigation properties on Promotion. I want to ignore these other navigation properties in one of my methods, and load only the promotions, each with a collection of products (code below is simplified).

If I use lazy loading:

// Lazy Loading true by default
// ctx.ObjectContext.ContextOptions.LazyLoadingEnabled = false;
var data = from p in ctx.ObjectContext.Promotions select p;

data.First().Products contains 1 Product.

If I use eager loading:

ctx.ObjectContext.ContextOptions.LazyLoadingEnabled = false;
var data = from p in ctx.ObjectContext.Promotions.Include(p => p.Products)
           select p;

data.First().Products is empty.

I have used eager loading elsewhere with success. I don't know why it isn't working for me here.

I realize that I can simply use linq joins to get the desired result, but this will complicate my class and cause some duplicated code. I can also force it using:

ctx.ObjectContext.ContextOptions.LazyLoadingEnabled = false;
var data = from p in ctx.ObjectContext.Promotions
           select new {
               Promo = p,
               Products = p.Products
          };

which gives 1 item in data.First().Promo.Products or data.First().Products, but I'd rather do it "by the book".

Entity Code

    public static Promotion CreatePromotion(global::System.Int32 id, ...)
    {
        Promotion promotion = new Promotion();
        promotion.Id = id;
        ...
        return promotion;
    }

...

    [XmlIgnoreAttribute()]
    [SoapIgnoreAttribute()]
    [DataMemberAttribute()]
    [EdmRelationshipNavigationPropertyAttribute("MyDataModel", "PromotionsProducts", "Product")]
    public EntityCollection<Product> Products
    {
        get
        {
            return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<Product>("MyDataModel.PromotionsProducts", "Product");
        }
        set
        {
            if ((value != null))
            {
                ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Product>("MyDataModel.PromotionsProducts", "Product", value);
            }
        }
    }
Kev
  • 2,656
  • 3
  • 39
  • 63
  • There has to be something else going on here because I tested this locally and it works fine with `LazyLoadingEnabled=false` and `Include(p => p.Products)` you need to check the rest of the code. – SOfanatic Mar 29 '16 at 17:17
  • I have a strong suspicion of what's going on here, but I really need to see the full class definitions. – Gert Arnold Mar 30 '16 at 20:59
  • @GertArnold I'll try to get that but will need to trim it down a little. In the meantime, tell me your suspicion and I can run some tests. – Kev Mar 31 '16 at 09:18
  • OK, I'm particularly interested in the constructors. It could be [this](http://stackoverflow.com/a/20773057/861716). – Gert Arnold Mar 31 '16 at 11:49
  • It's database-first, so my entities are auto generated, therefore there is no Promotion constructor. There is a CreatePromotion Factory Method. I've posted that above along with The PromotionsProducts Navigation property. Maybe my foreign keys are off. – Kev Mar 31 '16 at 14:43
  • Just checked my database schema and I think the FKs are correct. There is a linker table PromotionsProducts, which EF presents as the 2 navigation properties Promotion.Products and Product.Promotions. – Kev Mar 31 '16 at 15:00

0 Answers0