我有一个通过数据库优先导入生成的主/子关系模型。当我尝试在同一页面上输出子数据时,我会为每个子行而不是数据显示“System.Data.Entity.DynamicProxies ...”字符串。我认为 .NET MVC 足够聪明,可以识别子集合并自动输出行数据(我最终会添加显示/编辑格式化模板)。我可以通过主视图中的循环遍历数据,因此数据在集合中,但尝试 DisplayFor 子类正在显示代理名称或其他内容。有谁知道怎么回事?谢谢。
生成的模型(为简洁起见省略了额外的字段)如下:
namespace Test.Models
{
public partial class HCCB_CR
{
public HCCB_CR()
{
this.HCCB_CR_CONTACTS = new HashSet<HCCB_CR_CONTACT>();
}
public string CCID { get; set; }
public virtual ICollection<HCCB_CR_CONTACT> HCCB_CR_CONTACTS { get; set; }
}
}
namespace Test.Models
{
public partial class HCCB_CR_CONTACT
{
public decimal ID { get; set; } //key
public string CCID { get; set; } // foreign key to hccb_cr
public virtual HCCB_CR HCCB_CR { get; set; }
}
}
为详细信息生成的脚手架(缩写):
@model Test.Models.HCCB_CR
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<fieldset>
<legend>HCCB_CR</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.CCID)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.CCID)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.HCCB_CR_CONTACTS)
</div>
</fieldset>
输出:
CCID
HCCB-CR100290
System.Data.Entity.DynamicProxies.HCCB_CR_585E5CAF66461CDD0DF7F83DACDFCA848E78470297E638B38AB0FFDC8CBD83DASystem.Data.Entity.DynamicProxies.HCCB_CR_585E5CAF66461CDD0DF7F83DACDFCA848E78470297E638B38AB0FFDC8CBD83DASystem.Data.Entity.DynamicProxies.HCCB_CR_585E5CAF66461CDD0DF7F83DACDFCA848E78470297E638B38AB0FFDC8CBD83DASystem.Data.Entity.DynamicProxies.HCCB_CR_585E5CAF66461CDD0DF7F83DACDFCA848E78470297E638B38AB0FFDC8CBD83DASystem.Data.Entity.DynamicProxies.HCCB_CR_585E5CAF66461CDD0DF7F83DACDFCA848E78470297E638B38AB0FFDC8CBD83DASystem.Data.Entity.DynamicProxies.HCCB_CR_585E5CAF66461CDD0DF7F83DACDFCA848E78470297E638B38AB0FFDC8CBD83DA
什么是 System.Data.Entity.DynamicProxies.HCCB_CR_... 为什么我会得到?
更新:希望这可能会有所帮助。当我进入 DisplayFor 时调试输出:
The thread '<No Name>' (0x3e70) has exited with code 0 (0x0).
Step into: Stepping over non-user code 'System.Web.Mvc.WebViewPage<Test2.Models.HCCBJEFF_CR>.Html.get'
The thread '<No Name>' (0x3e48) has exited with code 0 (0x0).
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR.HCCBJEFF_CR_CONTACTS.get'
Step into: Stepping over non-user code 'System.Data.Entity.DynamicProxies.HCCBJEFF_CR_9D376C551350D68ED0D45F44495C7047639AE18E195454A7CE1DAB8EB66DDAF2.HCCBJEFF_CR_CONTACTS.get'
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR.CCID.get'
Step into: Stepping over non-user code 'System.Data.Objects.DataClasses.RelatedEnd.CreateSourceQuery<Test2.Models.HCCBJEFF_CR_CONTACT>'
Step into: Stepping over non-user code 'System.Data.Objects.DataClasses.RelatedEnd.ValidateLoad<Test2.Models.HCCBJEFF_CR_CONTACT>'
Step into: Stepping over non-user code 'System.Data.Objects.DataClasses.EntityCollection<Test2.Models.HCCBJEFF_CR_CONTACT>.Load'
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR_CONTACT.HCCBJEFF_CR_CONTACT'
Step into: Stepping over non-user code 'System.Data.Entity.DynamicProxies.HCCBJEFF_CR_CONTACT_0D5EDED0A9ECB95525658A667586E43649A2EDCBFD933981026D54A30B929661.HCCBJEFF_CR_CONTACT_0D5EDED0A9ECB95525658A667586E43649A2EDCBFD933981026D54A30B929661'
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR_CONTACT.CONTACTID.set'
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR_CONTACT.CNAME.set'
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR_CONTACT.CCID.set'
Step into: Stepping over non-user code 'System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly<Test2.Models.HCCBJEFF_CR_CONTACT>'
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR_CONTACT.CONTACTID.get'
Step into: Stepping over non-user code 'System.Data.Objects.ObjectStateManager.CheckKeyMatchesEntity'
Step into: Stepping over non-user code 'System.Data.Objects.ObjectStateManager.AddEntry'
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR_CONTACT.CNAME.get'
Step into: Stepping over non-user code 'System.Data.Objects.EntityEntry.TakeSnapshot'
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR_CONTACT.CCID.get'
Step into: Stepping over non-user code 'System.Data.Objects.EntityEntry.TakeSnapshot'
Step into: Stepping over non-user code 'System.Data.Objects.ObjectStateManager.AddEntry'
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR_CONTACT.HCCBJEFF_CR.get'
Step into: Stepping over non-user code 'System.Data.Entity.DynamicProxies.HCCBJEFF_CR_CONTACT_0D5EDED0A9ECB95525658A667586E43649A2EDCBFD933981026D54A30B929661.GetBasePropertyValue'
Step into: Stepping over non-user code 'System.Data.Objects.Internal.EntityProxyFactory.TryGetBasePropertyValue'
Step into: Stepping over non-user code 'System.Data.Objects.Internal.EntityProxyFactory.CreateBaseGetter.AnonymousMethod__4'
Step into: Stepping over non-user code 'System.Data.Objects.Internal.PocoPropertyAccessorStrategy.GetNavigationPropertyValue'
Step into: Stepping over non-user code 'System.Data.Objects.EntityEntry.WillNotRefSteal'
Step into: Stepping over non-user code 'System.Data.Objects.EntityEntry.FixupEntityReferenceToPrincipal'
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR_CONTACT.HCCBJEFF_CR.set'
Step into: Stepping over non-user code 'System.Data.Entity.DynamicProxies.HCCBJEFF_CR_CONTACT_0D5EDED0A9ECB95525658A667586E43649A2EDCBFD933981026D54A30B929661.HCCBJEFF_CR.set'
Step into: Stepping over non-user code 'System.Data.Objects.Internal.PocoPropertyAccessorStrategy.SetNavigationPropertyValue'
Step into: Stepping over non-user code 'System.Data.Objects.DataClasses.RelatedEnd.Add'
Step into: Stepping over non-user code 'System.Data.Objects.DataClasses.EntityReference<Test2.Models.HCCBJEFF_CR>.ReferenceValue.set'
Step into: Stepping over non-user code 'System.Data.Objects.DataClasses.EntityReference.SetEntityKey'
Step into: Stepping over non-user code 'System.Data.Objects.EntityEntry.FixupEntityReferenceToPrincipal'
Step into: Stepping over non-user code 'System.Data.Objects.EntityEntry.FixupReferencesByForeignKeys'
Step into: Stepping over non-user code 'System.Data.Objects.ObjectStateManager.FixupReferencesByForeignKeys'
Step into: Stepping over non-user code 'System.Data.Objects.ObjectStateManager.AddEntry'
Step into: Stepping over non-user code 'System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly<Test2.Models.HCCBJEFF_CR_CONTACT>'
Step into: Stepping over non-user code 'System.Data.Common.Internal.Materialization.Coordinator<Test2.Models.HCCBJEFF_CR_CONTACT>.ReadNextElement'
Step into: Stepping over non-user code 'System.Data.Common.Internal.Materialization.Shaper<Test2.Models.HCCBJEFF_CR_CONTACT>.SimpleEnumerator.MoveNext'
Step into: Stepping over non-user code 'System.Data.Objects.DataClasses.RelatedEnd.Merge<Test2.Models.HCCBJEFF_CR_CONTACT>'
Step into: Stepping over non-user code 'System.Data.Objects.DataClasses.EntityCollection<Test2.Models.HCCBJEFF_CR_CONTACT>.Load'
Step into: Stepping over non-user code 'System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad'
Step into: Stepping over non-user code 'System.Data.Objects.Internal.LazyLoadBehavior.LoadProperty<System.__Canon>'
Step into: Stepping over non-user code 'System.Data.Entity.DynamicProxies.HCCBJEFF_CR_9D376C551350D68ED0D45F44495C7047639AE18E195454A7CE1DAB8EB66DDAF2.HCCBJEFF_CR_CONTACTS.get'
Step into: Stepping over non-user code 'System.Web.Mvc.ModelMetadata.FromLambdaExpression<Test2.Models.HCCBJEFF_CR,System.Collections.Generic.ICollection<Test2.Models.HCCBJEFF_CR_CONTACT>>.AnonymousMethod__2'
Step into: Stepping over non-user code 'System.Web.Mvc.ModelMetadata.Model.get'
Step into: Stepping over non-user code 'System.Web.Mvc.Html.TemplateHelpers.TemplateHelper'
Step into: Stepping over non-user code 'System.Web.Mvc.Html.TemplateHelpers.TemplateFor<Test2.Models.HCCBJEFF_CR,System.Collections.Generic.ICollection<Test2.Models.HCCBJEFF_CR_CONTACT>>'
HCCB_CR_CONTACT 模板:
@model Test.Models.HCCB_CR_CONTACT
<fieldset>
<legend>HCCB_CR_CONTACT</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.ID)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.ID)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.CCID)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.CCID)
</div>
</fieldset>
<p>
@Html.ActionLink("Edit", "Edit", new { id=Model.ID }) |
@Html.ActionLink("Back to List", "Index")
</p>