I'm trying to refresh data within my EntityFramework ObjectSet but I'm receiving the following error: There is already an open DataReader associated with this Command which must be closed first.
So I have a ObjectContext which returns me an ObjectSet of customers. That list of customers I then stick into a grid. Below is a snippet from my method:
public class CustomerEntity : SECValidation.SECEntity
{
ObjectSet<Customer> objectCustomer;
public override void InitialiseObject()
{
objectCustomer = myObjectContext.CreateObjectSet<Customer>("Customers");
objectCustomer.MergeOption = MergeOption.OverwriteChanges;
}
public override List<T> SelectRecords<T>()
{
return (from custDetails in objectCustomer.Include("Owner_Lookup")
.Include("Business_Type_Lookup").Include("Assets").Include("Client")
select custDetails).Cast<T>().ToList();
}
public override T SelectRecordByUNID<T>(int UN_ID)
{
return (from custDetails in objectCustomer.Include("Owner_Lookup").Include("Business_Type_Lookup").Include("Assets").Include("Client")
where custDetails.UN_Customer == UN_ID
select custDetails).Cast<T>().SingleOrDefault();
}
}
So I create an instance of my CustomerEntity class and call InitialiseObject(). After that I call SelectRecords() which provides the results for my grid.
I have code on the SelectedIndex changed event on the grid which calls the method SelectRecordByUNID passing in the customerID of the selected row. This selects a customer record which is used elsewhere.
Now if somebody updates the data elsewhere (another instance of the app) I would like to refresh my objectCustomer to see those changes.
However the SelectRecords method runs OK, but when the SelectRecordByUNID method is called it fails with the error I mentioned above. The error says that there is an associated DataReader already open with this command and to close it. How can I get rounds this without closing the Data Reader as other data is opened which is related to the datasource of the customer grid?
I use myObjectContext to create object set for different entities too.
I'm using c# and .Net4.0
Help me please and I hope that makes sense!?!
Thanks.