无法将类型为“System.Web.UI.WebControls.EntityDataSourceWrapper”的对象强制转换为类型

时间:2011-06-28 07:00:29

标签: c# asp.net casting

我使用asp.net和c#。

我有一个DetailsView,我需要将数据绑定到它。

我收到此错误:您是否知道如何解决此问题? 谢谢你的时间。

请提供代码示例。

// Get the data for underlying DetailsView
DetailsView myDetailsView = ((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'. 

3 个答案:

答案 0 :(得分:5)

要从DataItem获取对象,您需要使用此处描述的方法:http://blogs.msdn.com/b/diego/archive/2008/05/13/entitydatasource-to-wrap-or-not-to-wrap.aspx 即创建类

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;
    }
}

然后致电

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

答案 1 :(得分:2)

在上面由Stas链接的Diego Vega的blog post中,引用了Colin Meek

  

为什么看到包装而不是实体?实体数据模型的一些独特功能阻止我们直接绑定实体。例如,在插入产品时,我还需要在类别中插入关系。包装器将关系添加到实体,基本上作为外键值。

如果您浏览ICustomTypeDescriptor,则可以获取实体。其他答案中的扩展类将为您完成此任务。

我喜欢在一行代码上做事情,所以我将模板放在下面。 e.Row.DataItem可以采用三种不同的形式,因此请使用哪种方法。下面的类型转换基本上就是扩展类所做的。

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

答案 2 :(得分:1)

看看here。用户共享他的扩展方法以将包装器转换为实体。

这是他的代码:

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;
    }
}
相关问题