0

I'm using an EntityDataSource. When I leave the "Select" property blank, all works great - in SomeWebControl_DataBinding I can perfectly convert the DataItem of the GridRow to my Entity (let's call it USERS).

Literal ctrlTime = (Literal)sender;
GridViewRow row = (GridViewRow)ctrlTime.NamingContainer;
USERS usersRow = (USERS)row.DataItem;
DateTime timestamp = usersRow.Time;

However I only need to show the 1st 1000 rows of the table so what I do is I fill the "Select" property of the EntityDataSource and set it to "TOP(1000) |list of all fields in the entity|". But as soon as I do that

    USERS usersRow = (USERS)row.DataItem;

fails miserably, telling me that it can't convert from MaterializedDataRecord to USERS.

What I ended up doing is this:

Literal ctrlTime = (Literal)sender;
GridViewRow row = (GridViewRow)ctrlTime.NamingContainer;
DbDataRecord usersRow = (DbDataRecord)row.DataItem;
DateTime timestamp = (DateTime)usersRow["Time"];

I could probably use (MaterializedDataRecord) instead of (DbDataRecord), I just didn't want to scare my colleagues. Now, this works, however I find it ugly and it sort of defeats the beauty of using the EntityFramework. Also, this is a very basic example, in the future I might face similar situation where I would need to box/unbox lots of other fields, which doesn't look good.

So my question is: is there any way of easily converting the MaterializedDataRecord to an entity or of setting the EntityDataSource not to consider the presence of something in the "Select" as a "threat" which leads to creating MaterializedDataRecord instead of an entity?

I mean I would understand that behavior if I was selecting a subset of the entity's fields, but here I'm selecting all the fields, so this shouldn't be a problem for the EntityDataSource to convert.

Thanks!

daniel
  • 1
  • 1

1 Answers1

0

As you can see here Performance of Linq to Entities vs ESQL, it appears that EntityDataSource uses ESQL to compose the query. Then when you use the "Select" property, it changes the projection and you can't use anymore the model Entity.

What you can do is use the QueryCreated event and filter the top 1000 records before the query is executed by changing it. There is an example here http://msdn.microsoft.com/en-us/library/ee404748.aspx.

Community
  • 1
  • 1
Luiz Felipe
  • 1,123
  • 8
  • 14