I am using DevExpress EntityInstantFeedbackSource
as datasource to an XtraGrid
control. However I am not using the connection string from the app.config
file; rather I am setting the connection string for entity framework at runtime.
The code is given below:
void Form1_Load(object sender, EventArgs e)
{
entityInstantFeedbackSource1.KeyExpression = "Prodid";
entityInstantFeedbackSource1.GetQueryable += entityInstantFeedbackSource1_GetQueryable;
entityInstantFeedbackSource1.DismissQueryable += entityInstantFeedbackSource1_DismissQueryable;
gridControl1.DataSource = null;
gridControl1.DataSource = entityInstantFeedbackSource1;
}
void entityInstantFeedbackSource1_GetQueryable(object sender, GetQueryableEventArgs e)
{
EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder();
ecsb.Metadata = @"res://*/Model2.csdl|res://*/Model2.ssdl|res://*/Model2.msl";
ecsb.Provider = @"System.Data.SqlClient";
ecsb.ProviderConnectionString = @"data source=.\sqlexpress;initial catalog=AdventureWorks; integrated security=True;MultipleActiveResultSets=True;App=EntityFramework";
using (var context = new ObjectContext(ecsb.ConnectionString))
{
context.DefaultContainerName = "AdventureWorksEntities";
ObjectSet<Person> query = context.CreateObjectSet<Person>();
var q = from s in query
select s;
e.QueryableSource = q;
e.Tag = context;
}
}
void entityInstantFeedbackSource1_DismissQueryable(object sender, GetQueryableEventArgs e)
{
((ObjectContext)e.Tag).Dispose();
}
The grid is blank. However if I write a foreach loop around 'query' and view the output in Console.WriteLine
then I can see the data.
Also if I set e.QueryableSource = q.ToArray().AsQueryable()
then I can see data in the grid. But doing this will load all data at one time there by nullifying the benefit of EntityInstantFeedbackSource
.
Why there is no data in query? And how to databind ObjectSet to a gridcontrol?
I believe the reason of this issue is that you are disposing the ObjectContext
directly in GetQueryable
handler rather then to do it in DismissQueryable
only. Moreover you can pass the resulting object set directly to e.QuerableSource
.
Thus the correct code should looks like this:
void entityInstantFeedbackSource_GetQueryable(object sender, DevExpress.Data.Linq.GetQueryableEventArgs e) {
//... connection initialization ...
var context = new ObjectContext(ecsb.ConnectionString);
ObjectSet<Person> personSet = context.CreateObjectSet<Person>();
e.QueryableSource = personSet;
e.Tag = context;
}
void entityInstantFeedbackSource_DismissQueryable(object sender, DevExpress.Data.Linq.GetQueryableEventArgs e) {
((ObjectContext)e.Tag).Dispose();
}