开发者

Unable to cast object of type 'System.Web.UI.WebControls.EntityDataSourceWrapper' to type

I use asp.net and c#.

I have a DetailsView and I need get the data bound to it.

I receive this error: Do you have an idea how to fix it? Thanks for your time.

Please provide me a sample of code.

// Get the data for underlying DetailsView
DetailsView myDetailsView = (开发者_StackOverflow(DetailsView)sender);
WebProject.DataAccess.DatabaseModels.CmsContent rowView (WebProject.DataAccess.DatabaseModels.CmsContent)myDetailsView.DataItem;

Unable to cast object of type 'System.Web.UI.WebControls.EntityDataSourceWrapper' to type 'WebProject.DataAccess.DatabaseModels.CmsContent'. 


To get object from DataItem you need to use approach described here: http://blogs.msdn.com/b/diego/archive/2008/05/13/entitydatasource-to-wrap-or-not-to-wrap.aspx i.e. create class

static class EntityDataSourceExtensions
{
    public static TEntity GetItemObject<TEntity>(object dataItem) where TEntity : class
    {
        var entity = dataItem as TEntity;
        if (entity != null)
        {
            return entity;
        }
        var td = dataItem as ICustomTypeDescriptor;
        if (td != null)
        {
            return (TEntity)td.GetPropertyOwner(null);
        }
        return null;
    }
}

And then call

var row = EntityDataSourceExtensions.GetItemObject<WebProject.DataAccess.DatabaseModels.CmsContent>(myDetailsView.DataItem)


In Diego Vega's blog post linked by Stas above, there is a quote from Colin Meek:

Why are you seeing a wrapper instead of the entity? Some of the unique features of the Entity Data Model prevent us from directly binding the entity. For instance, when inserting a Product I also need to insert a relationship to a Category. The wrapper adds the relationship to the entity, basically as a foreign key value.

You can get the Entity if you go through the ICustomTypeDescriptor. The extension classes in the other answers will do this for you.

I like to do things on one line of code, so I keep the template below handy. e.Row.DataItem can take on three different forms, so use whichever method works. The type conversions below are basically what the extension classes do.

If e.Row.RowType = DataControlRowType.DataRow Then

    'try this method first, simply get the entity
    Dim myCustomer = CType(e.Row.DataItem, Customer)

    'if you get a EntityDataSourceWrapper error, try getting the entity through the ICustomTypeDescriptor
    'Dim myCustomer = CType(CType(e.Row.DataItem, ComponentModel.ICustomTypeDescriptor).GetPropertyOwner(Nothing), Customer)

    'if you get a MaterializedDataRecord error, you cannot get the entity, so treat your DataItem like a DataRow
    'Dim rowCustomer = CType(e.Row.DataItem, Data.Common.DbDataRecord)

    'do stuff with your entity
    Dim CustomerID As Integer = myCustomer.CustomerID
    Dim FirstName As String = myCustomer.FirstName

    'if no entity was available, you have to use the DbDataRecord
    'Dim CustomerID As Integer = rowCustomer("CustomerID")
    'Dim FirstName As String = rowCustomer("FirstName")
End If


Take a look here. There users shares his extension methods to convert wraper to entity.

Here is his code:

public static class EntityDataSourceExtensions
{
    public static TEntity GetEntityAs<TEntity>(this object dataItem) where TEntity : class
    {
        var entity = dataItem as TEntity;

        if (entity != null)
            return entity;

        var td = dataItem as ICustomTypeDescriptor;

        if (td != null)
            return (TEntity)td.GetPropertyOwner(null);

        return null;
    }

    public static Object GetEntity(this object dataItem)
    {
        var td = dataItem as ICustomTypeDescriptor;

        if (td != null)
            return td.GetPropertyOwner(null);

        return null;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜