peter
peter

Reputation: 2103

Entity Framework: alternatives to using MultipleActiveResultSets

I'm using ASP.NET WebAPI and ran into a problem with a nested model that should be communicated via a WebAPI Controller:

The entities "bond, stock etc." each have a list of entities "price". Server-side, I use the following class to match this requirement..

public class Bond : BaseAsset
{
    public int ID { get; set; }
    public string Name { get; set; }

    public virtual List<Price> Prices { get; set; }
}

This leads to the table "Price" having a column for bond, stock etc. and, in case a price is attached to a bond, an entry in its column for bond foreign key.

The error I initially got was

There is already an open DataReader associated with this Command

I fixed that by altering the Connection String to allow MultipleActiveResultSets.

However, I feel there must be better options or at least alternatives when handling nested models. Is it, e.g., a sign for bad model design when one runs into such a problem? Would eager loading change anything?

Upvotes: 1

Views: 262

Answers (1)

Alexandre Rondeau
Alexandre Rondeau

Reputation: 2687

One alternative to mars is to disable lazy loading

In your DbContext

Configuration.LazyLoadingEnabled = false;

plus when you are loading your data you can explicit load your child tables

context.Bonds.Include(b => b.Prices)

Upvotes: 2

Related Questions