I got a problem with lazy loading in combination with a foreach loop.I'm working on a code first asp.net backend for a website, and sometimes I want to gather information spanning multiple tables/models.For example, take this:public class Foo{    [Key]    public Int32 Id { get; set; }    public virtual Bar Bar { get; set; }    public Int32 someInt { get; set; }}public class Bar{    [Key]    public Int32 FooId { get; set; }    [ForeignKey(FooId)]    public virtual Foo Foo { get; set; }    public String someString { get; set; }}public async Task<IHttpActionResult> getSomeInformation{    using(WhateverContext context = new WhateverContext())    {        var Foos = context.Foos;        List<FrontendFriendlyModel> ForTheFrontend = new List<FrontendFriendlyModel>();        foreach (var Foo in Foos)        {            var entry = new FrontendFriendlyModel();            entry.Id = Foo.Id; //Works fine            entry.someInt = Foo.someInt; //Works also fine            entry.someString = Foo.Bar.someString; //Crashes, There is already an open DataReader associated with this Command            ForTheFrontend.Add(entry);        }        return Ok(ForTheFrontend); //I convert it to a Json String with Json.net, but thats not relevant I guess    }}I know I could activate MARS in my connection String.I could also eager load Bar on the spot, and prevent the problem.var Foos = context.Foos.Include(f => f.Bar);From everything I gathered, using MARS is generally not considered good practice, and I don't like that I have to eager load everything in the at the start .Things I could find on google about this, don't really help me here (Maybe I just suck at googling), and I feel like I'm missing something really basic.So I'm wondering, whats the correct way here. Activating MARS? Eager Load everything I need? Cast the IQueryable to a List?        var Foos = context.Foos.ToList();        foreach (var Foo in Foos)        {            var entry = new FrontendFriendlyModel();            entry.Id = Foo.Id; //Works fine            entry.someInt = Foo.someInt; //Works also fine                            entry.someString = Foo.Bar.someString; //Now that works, but I don't get why it doesnt work as IQueryable.            ForTheFrontend.Add(entry);        }I don't understand why I can't work with an IQueryable here, so maybe someone can enlighten me.
Iterating through IQueryable with lazyloading results in multiple open DataReaders
c#;asp.net
You have two choices:You can eager load, orYou can enable MARS.Presumably, you want to use an IQueryable to obtain deferred execution.  I assume you want to call getSomeInformation() with await.  If you want deferred lookup from the database, you must use MARS; you don't have a choice.However, note that you can still get most of the benefits of deferred execution by eager-loading the related entity.  Your method will still be async.  The only real difference will be that you are pre-loading the entity into memory instead of retrieving it from the database on-demand later on, which is probably what you want anyway.As to whether or not the use of MARS is a good or bad practice, I think it comes down to understanding how to use it properly.  It's easy to do eager loading correctly; it's a bit harder with MARS.  Consider these errors that can occur with MARS:The transaction operation cannot be performed because there are pending requests working on this transaction.The server failed to resume the transaction. The transaction active in this session has been committed or aborted by another session.The underlying provider failed on Open. The connection was not closed. The connection's current state is connecting.ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.The server failed to resume the transaction.All of these errors demonstrate fundamental misunderstandings about how MARS works, and how the underlying connection and transaction model should be used.  Microsoft has adequate guidance (linked below) for using MARS in ways that won't cause these errors.Further ReadingUsing Multiple Active Result SetsEnabling Multiple Active Result SetsExceptions when Using MARS