0

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

我在哪里搞砸了?

它是这样的:

[控制器.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;
    }
4

2 回答 2

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) 来解决(也许)。但这并不能解决您将执行潜在大量查询(每个物化模型两个查询)的事实。

于 2013-11-10T21:00:51.240 回答
0

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

于 2013-11-10T06:35:32.570 回答