MySQL Code First Lazy Load问题!

在一个非常简单的房地产程序中,我正在尝试使用附加到现有数据库的EF Code First列出房屋的所有图像,我使用的是
MySQL Conector 6.3.6,这是我的代码.

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; }
    }
}

当我尝试运行它时,我收到以下错误:

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 —

有趣的是,如果我使用以下查询

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

然后它可以工作但是执行了一个急切的加载,因为当我检查每个房子的Images属性时,它们已被填充.所以,我的问题是,Lazy Load可以与MySQL和Code First一起使用吗?

我知道Linq在我们使用它之前不执行查询,所以当懒惰加载图像时,DataReader可能仍然打开,这就是问题发生的原因.

对这个问题的任何启示都表示赞赏.

谢谢
安德森福塔莱萨

最佳答案

…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.

这正是发生的事情,但我认为并不是你想的原因. DataReader仍处于打开状态,不是因为Linq中的延迟执行,而是因为当您尝试访问尚未加载的其他属性时,您仍在迭代查询结果.当您调用.ToList()时,结果将立即返回并存储在List< TEntity>中.在客户端的内存中,而不是一次返回1条记录.

您可以在连接字符串中使用设置MultipleActiveResultSets = true在MS SQL Server中解决此问题,但MySQL不支持此设置.但是,您应该能够做的是急需加载您需要的其他数据.Include(“tablename”)

var houses = (from h in db.Houses.Include("Images")
              select h).Take(10);
点赞