Search code examples
c#castingentitydatasource

Unable to cast object of type 'System.Data.Objects.MaterializedDataRecord' to type 'myEntity' error


I am using an EntityDataSource. I want to access the results and convert them to a List. To do that I'm doing the following code:

 protected void EntityDatSrc_Selected(object sender, EntityDataSourceSelectedEventArgs e)
    {
        var statHistLst = e.Results.Cast<MyEntity>().ToList();
    }

This code I got from the question and answer here: How to access EntityDataSource selected data programatically.

I'm getting this error

System.InvalidCastException: Unable to cast object of type 'System.Data.Objects.MaterializedDataRecord' to type 'MySoluton.DAL.MyEntity'

What's causing this problem and how can I fix it?


Solution

  • The problem apperently was Entity Framework 6 which has problems with the old EntityDataSource control.

    So I switched to the new EntityDataSource control and made changes according to instructions given HERE in Sergey's answer and LMK's comment there. That solved the problem.