Open Datareader可防止POCO被填充

时间:2013-11-10 05:24:15

标签: c# mysql entity-framework

我正在尝试使用查询结果填充POCO到我的数据库。结果集有多行,所以我只是迭代每个结果并用数据填充POCO,我得到这个错误:“已经有一个与此Connection关联的开放DataReader必须先关闭。”

我在哪里弄乱?

它是这样的:

[Controller.cs]

        /// <summary>
        /// Populates the inner Ads list
        /// </summary>
        public void FetchAds()
        {
            _ads = new List<Ad>();
            using (var context = (ApartmentDataEntities) DbContextFactory.GetInstance().GetDbContext<ApartmentDataEntities>())
            {
                foreach (var config in context.Configurations)
                {
                    _ads.Add(new AdModel(_basePath, config));
                }

            }

            AdsReady.SafeTrigger(this, new AdArray { Ads = _ads.ToArray() });
        }

[AdModel.cs] (继承自POCO)

    public AdModel(String baseFolder, Configuration apartment)
    {
        _baseFolder = baseFolder;
        GetAd(apartment);

    }


    /// <summary>
    /// Populates the Ad from the Database
    /// </summary>
    private void GetAd(Configuration apartment)
    {

        PropertyId = apartment.Property.id;
        PropertyName = apartment.Property.name;
        PropertyPhone = apartment.Property.phone;
        PropertyAddress = apartment.Property.address;
        AreaName = apartment.Property.MapArea.areaName;
        RegionName = apartment.Property.MapArea.Region.name;
        PropertyZipCode = apartment.Property.zipCode;
        ComissionRate = apartment.Property.comissionRate;
        Images = apartment.Property.Images.Select(img => img.id).ToArray();
        YearBuilt = apartment.Property.yearBuilt;
        Features = apartment.Property.features;
        Ammenities = apartment.Property.ammenities;
        CommunitySpecial = apartment.Property.communitySpecial;
        PetPolicy = apartment.Property.petPolicy;
        Size = apartment.size;
        Bathrooms = apartment.bathrooms;
        Bedrooms = apartment.bedrooms;
        Price = apartment.price;
        PropertyImages = apartment.Property.Images.Select(img => img.imageContents).ToArray();
        FloorplanImage = null;
        Floorplan = null;

        var configFloorplan = apartment.Images.SingleOrDefault();
        if (configFloorplan == null) return;

        FloorplanImage = configFloorplan.imageContents;
        Floorplan = configFloorplan.id;
    }

2 个答案:

答案 0 :(得分:1)

通常,在查询本身中对另一个模型进行投影是一个更好的主意。像

_ads = (from apartment in context.Configurations
        let configFloorplan = apartment.Images.SingleOrDefault()
        select new AdModel
        {
            PropertyId = apartment.Property.id,
            PropertyName = apartment.Property.name,
            PropertyPhone = apartment.Property.phone,
            ...
            PropertyImages = apartment.Property.Images
                                      .Select(img => img.imageContents),
            FloorplanImage = configFloorplan.imageContents,
            Floorplan = configFloorplan.id
        }).ToList();

这可确保所有内容都作为一个查询执行。您的方法存在的问题是,当EF正在读取context.Configurations时,会执行其他查询以填充模型的其他属性。

这可以通过在连接字符串中启用多个活动结果集(MARS)来解决(可能)。但这并不能解决您将执行潜在大量查询的事实(每个物化模型有两个查询)。

答案 1 :(得分:0)

您使用的是EntityFramework吗?可能是GetAdd中的.Select是延迟加载,因此在外部查询仍处于打开状态时尝试查询数据库。尝试在context.Configurations之后添加toArray()。似乎我不得不把这些洒在这个地方以避免这个错误。