Reputation: 99
I have a subscription table, and at first we had a direct mapping with a class named Subscription. We changed that mapping, so that now the SubscriptionDto class maps to the subscription table.
The changes I have made to the DatabaseContext class:
For the CompanyRepresentation, there's this piece of configuration in the DatabaseContext:
The SubscriptionDto class is identical to the Subscription class, the only thing the Subscription class doesn't have is the virtual keyword on every collection property. It looks like this:
In the code it used the Subscription class to the database and from the database, but since it got replaced with the SubscriptionDto class, a conversion needs to be done. This is done in the repository class SubscriptionsInSql:
The code that does the conversion from SubscriptionDto to Subscription throws an error when trying to assign subscriptionDto.CompanyRepresentation to CompanyRepresentation: "There's already an open DataReader associated with this Command which must be closed first".
If I look at what is retrieved in the ForAccountWith method, I see that there's an exception as well:
I've tried many things, like removing the virtual keywords, data annotations, etc., but I can't seem to figure the thing out that makes this work. Some help would be appreciated!
Upvotes: 1
Views: 114
Reputation: 93444
The error "There's already an open DataReader associated with this Command which must be closed first" typically means you have not enabled Multiple Active Result Sets (MARS) in your connection string.
For instance:
Data Source=<db>;Initial Catalog=db;Integrated Security=SSPI;MultipleActiveResultSets=True
However, looking at your code, I think you have misinterpreted your requirements. It seems like you are trying to replace Subscription with SubscriptionDto, but that's not the way DTO patterns typically work. Almost certainly what is required is that you translate Subscription to SubscriptionDto in your repository layer. Dto's are Data Transfer Objects and are used as intermediaries between your data layer and you business or other layers. This decouples your data layer from your other layers.
Upvotes: 1
Reputation: 7800
Please note that Find theoretically only retrieve one entity.
please try :
List<Subscription> res = new List<Subscription>();
foreach (SubscriptionDto dto in repository.Where(x => x.AccountId == accountId)) {
res.Add(CreateSubscription(dto));
}
return res;
Upvotes: 1