Andre Roque
Andre Roque

Reputation: 543

Lazy Loading on WPF app

I'm building a WPF application, and I'm using a WebService to retrieve data from Database using Entity Framework. On my service the code generated is:

public partial class DummyEntities : ObjectContext
    {
        public DummyEntities() : base("name=DummyEntities", "DummyEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }

//more 2 constructors with diferent signature but the same code

So the Lazy Loading option is enabled.

The problem is on the app side, when I do something like this:

DummyEntities context = new DummyEntities(Utils.GetUri());
//            ...
context.Order.Order_Details.Count

give me count = 0, when it shouldn't because Lazy loading is active. Am I missing something here?

Upvotes: 0

Views: 784

Answers (2)

Andre Roque
Andre Roque

Reputation: 543

Thanks for the hint Daniel. I searched a little more and I found the LoadProprety method. That solves the problem. Thanks

Edit:

The code I used was:

IEnumerable<Order> orders = context.Order.AsEnumerable<Order>();

        foreach (var order in orders)
        {
            context.LoadProperty(order, "Order_Details");
            result.Add(new Order(order));
        }

This way I'm forcing each order to load the "Order_Details" related entity

Upvotes: 0

Daniel
Daniel

Reputation: 632

If you've crossed a service boundary, the entities have been serialized and deserialized to another object, so they've lost the connection to the db

All lazy loading has to be done before the data is transferred

Upvotes: 2

Related Questions