Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4601740
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:52:23+00:00 2026-05-21T23:52:23+00:00

in a very simple real estate program I’m trying to list all images for

  • 0

in a very simple real estate program I’m trying to list all images for a house using EF Code First attached to an existing DB I have, I’m using MySQL Conector 6.3.6, here’s my code.

namespace CodeFirstMySQL
{
    class Program
    {
        static void Main(string[] args)
        {
            RealEstate db = new RealEstate();

            var houses = (from h in db.Houses
                         select h).Take(10);

            foreach (var house in houses)
            {
                Console.WriteLine(string.Format("Images for {0}", house.Address));
                foreach (Image image in house.Images)
                {
                    Console.WriteLine(string.Format("=> {0}", image.FileName));
                }
            }
        }
    }

    public class RealEstate : DbContext
    {
        public DbSet<House> Houses { get; set; }
        public DbSet<Image> Images { get; set; }
    }

    [Table("CADIMO")]
    public class House
    {
        [Column("CODIGO")]
        public int HouseId { get; set; }

        [Column("ENDERECO")]
        public string Address { get; set; }

        public virtual ICollection<Image> Images { get; set; }
    }

    [Table("CDIMIM")]
    public class Image
    {
        [Key]
        [Column("CODIGO", Order = 0)]
        public int HouseId { get; set; }

        [Key]
        [Column("CODIGO_I", Order = 1)]
        public int ImageOrder { get; set; }

        [Column("FILE_PATH")]
        public string FileName { get; set; }
    }
}

When I try to run this I get the following error:

Images for Porto das Dunas
Unhandled Exception:
System.Data.EntityCommandExecutionException:
An error occurred while executing the
command definition. See the inner
exception for details.

—> MySql.Data.MySqlClient.MySqlException:
There is already an open DataReader
associated with this Connection which
must be closed first.

at
MySql.Data.MySqlClient.MySqlCommand.CheckState()
at
MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior
behavior)
at
MySql.Data.Entity.EFMySqlCommand.ExecuteDbDataReader(CommandBehavior
behavior) at
System.Data.Common.DbCommand.ExecuteReader(CommandBehavior
behavior) at
System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand
entityCommand, CommandBehavior
behavior)
— End of inner
exception stack trace —

The interesting thing is that if I use the following query

List<House> houses = (from h in db.Houses select h).Take(10).ToList();

Then it works but a eager load is executed because when I check the Images property for each house they are already populated. So, my question is, can Lazy Load be used with MySQL and Code First?

I know that Linq doesn’t execute the query until we use it, so maybe the DataReader is still open when it’s time to lazy load the images and this is why the problem is taking place.

Any light on this issue is appreciated.

thank you
Anderson Fortaleza

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:52:24+00:00Added an answer on May 21, 2026 at 11:52 pm

    …so maybe the DataReader is still open when it’s time to lazy load the images and this is why the problem is taking place.

    That is exactly what’s happening, but I think for not quite the reason that you think. The DataReader is still open, not because of deferred execution in Linq, but because you’re still iterating through the query results when you try to access the other property that isn’t loaded yet. When you call .ToList() the results get returned all at once and stored in a List<TEntity> in memory on the client, instead of being returned 1 record at a time.

    You can get around this in MS SQL Server using the setting MultipleActiveResultSets=true in your connection string, but MySQL doesn’t support this setting. What you should be able to do, though, is eager-load the additional data you need using .Include("tablename")

    var houses = (from h in db.Houses.Include("Images")
                  select h).Take(10);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Summary When I execute a very simple program using Perl's Benchmark utility. I get
I'm trying to make a very simple iphone app that displays the real-time camera
I am trying to implement a very simple file transfer client in python using
I am trying to create a very simple application (part of my learning program),
Very simple question, I made the following program : #include <stdlib.h> int main(int argc,
Very simple Winforms application I want to deploy manually. Can all the referenced assemblies
Very simple question, but I want to start using a consistent naming convention for
This very simple code gives me tons of errors: #include <iostream> #include <string> int
I have a very simple C++ code sample here: char *s = new char[100];
On Windows Server 2008 R2 the following very simple program behaves different on system

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.