问题描述
我使用EF6 / .Net 4.5.1来检索webform中usercontrol中的listview的数据。我修改了ApplicationUser,使用一个非常好的FK属性来包含一个导航属性[1:1]。
I am using EF6/.Net 4.5.1 to retrieve data for a listview in a usercontrol on a webform. I have modified the ApplicationUser to include a Navigation Property [1:1] using a FK attribute which has been working great.
public class ApplicationUser : IdentityUser
{
public ApplicationUser()
{
CreateDate = DateTime.Now;
:\\ deleted stuff
}
public int TaxID { get; set; }
public System.Guid ApplicationId { get; set; }
:\\ deleted stuff
[ForeignKey("TaxID")]
public virtual Personnel Personnel { get; set; }
}
模型已经迁移并进行了测试以进行存储和检索。
The model was migrated and tested for storing and retrieving.
一切都可以正常完成回发。
Everything works fine with a full postback.
然而,我在网页上添加了一个按钮,打开和关闭一个负责创建新会员的UserControl的div。 UserControl抛出容器所消耗的事件。然后容器关闭包含UC的div,用Listview重新打开div,调用调用GetAllUsers()的Listview DataBind()。
However I added a button on the webpage that opens and closes a div which holds a UserControl responsible for creating a new Member. The UserControl throws an event that is consumed by the container. The Container then closes the div containing the UC, reopens the div with the Listview, calls the Listview DataBind() which calls the GetAllUsers().
代码: p>
The code:
public IQueryable<ApplicationUser> GetAllUsers()
{
var manager = HttpContext.Current.GetOwinContext()
.GetUserManager<ApplicationUserManager>();
var users = manager.Users.OrderBy(c => c.TaxID);
return users;
}
UserControl将控件返回到容器后出现问题。
A problem arises after the UserControl returns control to the Container.
第一个检索 - 总是有一个ApplicationUser,没有加载Navigation属性。这意味着某些字段永远不会在列表视图中填充。
The first retrieve - always has an ApplicationUser which does NOT have the Navigation property loaded. This means certain fields are never populated in the listview.
随后的检索(刷新页面或调用行的编辑),但是似乎触发导航属性为现在。
Subsequent Retrieves (a refresh of the page or calling Edit of a row) however, seem to trigger navigation properties to be present.
-
如何强制EF包含导航属性。语法manager.Users.Include()在此上下文中不存在。
How can I force EF to Include the navigational property. The syntax manager.Users.Include() does not exist in this context.
只有作为ApplicationUser动态代理列出的实体似乎具有导航属性。所以我很神秘,为什么初始检索永远不是一个动态的。
Only the entities listed as dynamicproxies of ApplicationUser seems to have the navigational property present. So I'm mystified why the initial retrieve is Never a dynamicproxy.
listview的Datapager需要一个IQueryable来实现其分页。我不会调用.ToList()作为Datapager知道什么时候这是一直很好的...一旦有一个完整的页面刷新。为什么需要页面刷新才能实现导航属性?
The listview's Datapager requires an IQueryable to implement its paging. I do not call .ToList() as the Datapager knows when to This has been working great ... once there is a full page refresh. Why would a page refresh be required for the navigational properties to materialize?
任何帮助...提前感谢。
Any help ... thanks in advance ...
推荐答案
添加
using System.Data.Entity;
然后你可以使用 Include
/ p>
and then you can use Include
:
var users = manager.Users.Include(x=> x.Personnel).OrderBy(c => c.TaxID);
如果要在任何地方包含导航,请在 IdentityConfig.cs
文件覆盖
ApplicationUserManager.Users
像这样:
If you want to include the navigation anywhere, in IdentityConfig.cs
file override ApplicationUserManager.Users
like this:
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public override IQueryable<ApplicationUser> Users
{
get
{
return base.Users.Include(x=>x.Personnel);//include what you want here
}
}
//Rest of the original code
}
这篇关于强制EF ApplicationUser加载导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!