开发者

Unable to cast object of type 'MyType' to type 'Castle.Proxies.MyType'

开发者 https://www.devze.com 2023-01-05 19:27 出处:网络
In which situation following error occurs? Unable to cast object of type \'MyType\' to type \'Castle.Proxies.MyType\'.

In which situation following error occurs?

Unable to cast object of type 'MyType' to type 'Castle.Proxies.MyType'.

UPDATE:

I got this error message while binding MyType.FindAllByProperty("col1", "foo"); to a GridView (exactly a Telerik's one: RadGrid). This error is not occuring every time, just sometimes. Here is markup of my grid:

<telerik:RadGrid ID="grdList" runat="server" AutoGenerateColumns="false" OnNeedDataSource="grdList_NeedDataSource"
    OnUpdateCommand="grdList_UpdateCommand" AllowAutomaticUpdates="True" OnItemUpdated="grdList_ItemUpdated"
    AllowMultiRowEdit="true">
    <MasterTableView EditMode="InPlace" DataKeyNames="PageInRole_id" AllowAutomaticUpdates="true">
        <Columns>
            <telerik:GridBoundColumn DataField="ContainerPage.PageTitle" HeaderText="Title" UniqueName="ContainerPage.PageTitle"
                ReadOnly="true" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

UPDATE2

Here is all exception message and stack trace:

Unable to cast object of type 'PineCMS.Core.PageInRole' to type 'Castle.Proxies.PageInRoleProxy'. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidCastException: Unable to cast object of type 'PineCMS.Core.PageInRole' to type 'Castle.Proxies.PageInRoleProxy'.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[InvalidCastException: Unable to cast object of type 'PineCMS.Core.PageInRole' to type 'Castle.Proxies.PageInRoleProxy'.]
   Telerik.Web.UI.GetEnumerator>d__0.MoveNext() +175
   Telerik.Web.UI.GridDataTableFromEnumerable.FillDataTableFromEnumerable(IQueryable enumerable) +1285
   Telerik.Web.UI.GridDataTableFromEnumerable.FillData35() +3390
   Telerik.Web.UI.GridDataTableFromEnumerable.FillData() +824
   Telerik.Web.UI.GridResolveEnumerable.EnsureInitialized() +28
   Telerik.Web.UI.GridEnumerableFromDataView..ctor(GridTableView owner, IEnumerable enumerable, Boolean CaseSensitive, Boolean autoGenerateColumns, GridColumnCollection presentColumns, String[] additionalField, Boolean retrieveAllFields) +242
   Telerik.Web.UI.GridDataSourceHelper.CreateGridEnumerable(GridTableView owner, IEnumerable enumerable, Boolean caseSensitive, Boolean autoGenerateColumns, GridColumnCollection presentColumns, String[] additionalField, Boolean retrieveAllFields) +223
   Telerik.Web.UI.GridDataSourceHelper.GetResolvedDataSource(GridTableView owner, Object dataSource, String dataMember, Boolean caseSensitive, Boolean autoGenerateColumns, GridColumnCollection presentColumns, String[] additionalField, Boolean retrieveAllFields) +595
   Telerik.Web.UI.GridTableView.get_ResolvedDataSource() +243
   Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource) +42
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +72
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +147
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +261
   Telerik.Web.UI.GridTableView.PerformSelect() +23
   Telerik.Web.UI.GridTableView.DataBind() +363
   Telerik.Web.UI.GridTableView.Rebind() +101
   Telerik.Web.UI.GridCommandEventArgs.ExecuteCommand(Object source) +1174
   Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e) +185
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +70
   Telerik.Web.UI.GridItem.OnBubbleE开发者_运维技巧vent(Object source, EventArgs e) +142
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +70
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927


This exception probably comes from the code trying to re-attach a proxied/lazy-loading entity to a session. The most likely API call to throw this exception with a newer NHibernate (I don't have problem with 2.1 but I do have problems with latest) is Session.Lock(obj, LockMode.None).

If the programmer of this component first evicts the entity from the ISession and then re-loads it (perhaps cached?) into another one by id, or performs ISession.Update this problem should go away until the NHibernate team has figured out the underlying problem.


I had a similar problem with EntityFramework 6.0. I had migrated from an old version of EntityFramework that did not use proxies and had some code that created and attached new instances of the entity via new MyEntity();.

My solution was to use dbContext.MyEntity.Create(); instead so I only work with proxies. Something similar should be available for NHibernate if I recall correctly.

My error was:

Unable to cast object of type 'MyProject.MyEntity' to type 'System.Data.Entity.DynamicProxies.MyEntity_849E94C98E5E543D6DF5245252144E3EAC00131F811886276B6ABD991719D232'


We ran into the same issue with RadGrid having AutomaticInserts. After inserting an item it throw InvalidCastException: Unable to cast object of type 'MyDbModel.Role' to type 'System.Data.Entity.DynamicProxies.Role_...' in DataBind() at Rebind().

The issue was gone after I adapted the DataBinding to have an OrderBy().

0

精彩评论

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

关注公众号