实体框架跟踪集合可以绑定到ASP.Net Gridview? [英] Can a Entity Framework Trackable collection be bound to an ASP.Net Gridview?
本文介绍了实体框架跟踪集合可以绑定到ASP.Net Gridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
this.gvMyGridView.DataSource = entity.MyDetailedItems;
this.gvMyGridView.DataBind();
但这并不显示任何数据。
我正在使用自我跟踪实体,MyDetailedItems是另一个表中的行的导航属性。
解决方案
自我跟踪实体不支持延迟加载因此,如果要使用它们,则必须显式加载导航属性。使用:
//加载具有相关实体的实体
var entity = context.Entities.Include(MyDetailedItems )。单(...);
或
//为已加载实体加载相关实体
context.LoadProperty(entity,MyDetailedItems);
I've got a GridView on a ASP.Net page. I would like to set the DataSource of the Gridview to a trackable collection of Entity Framework objects. I though the code should look like this:
this.gvMyGridView.DataSource = entity.MyDetailedItems;
this.gvMyGridView.DataBind();
But this doesn't display any data.
I am using self tracking entities and the MyDetailedItems is a navigation property to rows from another table.
解决方案
EF 4 with self tracking entities does not support lazy loading so you must explicitly load navigation properties if you want to use them. Use either:
// loading entity with related entities
var entity = context.Entities.Include("MyDetailedItems").Single(...);
or
// loading related entities for already loaded entity
context.LoadProperty(entity, "MyDetailedItems");
这篇关于实体框架跟踪集合可以绑定到ASP.Net Gridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文