发生以下错误的情况?
无法将“MyType”类型的对象转换为“Castle.Proxies.MyType”类型。
更新
我将MyType.FindAllByProperty("col1", "foo");
绑定到GridView
(确切地说是Telerik的一个:RadGrid
)时收到此错误消息。每次都不会发生此错误,有时候也是如此。这是我网格的标记:
<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
这是所有异常消息和堆栈跟踪:
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.OnBubbleEvent(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
答案 0 :(得分:2)
此异常可能来自尝试将代理/延迟加载实体重新附加到会话的代码。使用较新的NHibernate抛出此异常的最可能的API调用(我没有2.1的问题,但我确实遇到最新的问题)是Session.Lock(obj, LockMode.None)
。
如果这个组件的程序员首先从ISession驱逐实体,然后通过id重新加载它(可能是缓存?)到另一个实体,或者执行ISession.Update
这个问题应该消失,直到NHibernate团队有找出了潜在的问题。
答案 1 :(得分:0)
我在EntityFramework 6.0中遇到了类似的问题。
我从一个不使用代理的旧版EntityFramework迁移过来,并且有一些代码通过new MyEntity();
创建并附加了实体的新实例。
我的解决方案是使用dbContext.MyEntity.Create();
代替,所以我只使用代理。如果我没记错的话,应该为NHibernate提供类似的东西。
我的错误是:
无法投射类型&#39; MyProject.MyEntity&#39;输入 &#39; System.Data.Entity.DynamicProxies.MyEntity_849E94C98E5E543D6DF5245252144E3EAC00131F811886276B6ABD991719D232&#39;