我正在尝试使用对我的数据库的查询结果填充 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;
}