【发布时间】:2015-04-16 21:43:43
【问题描述】:
在我的上下文中访问第三级导航属性时遇到问题。我已经搜索了两天但是 找不到任何靠近我的问题。所以我认为我的方法存在逻辑错误。
我使用 MVC 模板项目。模型类和上下文如下。
public partial class Tests
{
[Key]
public int TestID { get; set; }
public string TestName { get; set; }
public List<UserTests> UserTests { get; set; }
}
public partial class UserTests
{
[Key, Column(Order=1)]
public string UserID { get; set; }
[Key, Column(Order = 2)]
public int TestID { get; set; }
public Nullable<double> TestValue { get; set; }
public ApplicationUser ApplicationUser { get; set; }
public Tests Tests { get; set; }
}
public partial class UserDetail
{
[Key]
public int ID { get; set; }
public string UserID { get; set; }
public string Name { get; set; }
public ApplicationUser ApplicationUser { get; set; }
}
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public List<UserTests> UserTests { get; set; }
public List<UserDetail> UserDetails { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
this.Configuration.ProxyCreationEnabled = false;
this.Configuration.LazyLoadingEnabled = false;
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public IDbSet<Tests> Tests { get; set; }
public IDbSet<UserDetail> UserDetails { get; set; }
public IDbSet<UserTests> UserTests { get; set; }
}
我尝试从UserTests 访问UserDetails 属性。但是我在Include 方法中找不到Select 方法,点击ApplicationUser 之后的点。
public ActionResult Index()
{
var userTests = db.UserTests.Include(u => u.Tests).Include(y => y.ApplicationUser.UserDetails);
return View(userTest.ToList());
}
这是索引视图。
@model IEnumerable<WebApplication6.Models.UserTests>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ApplicationUser.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.Tests.TestName)
</th>
<th>
@Html.DisplayNameFor(model => model.TestValue)
</th>
</tr>
@foreach (var item in Model) {
<tr>
<th>
@Html.DisplayFor(modelItem => item.ApplicationUser.Email)
</th>
<td>
@Html.DisplayFor(modelItem => item.Tests.TestName)
</td>
<td>
@Html.DisplayFor(modelItem => item.TestValue)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
我希望视图显示 ApplicationUser 的名称,而不是 UserDetails 模型类中的电子邮件。所以我想输入如下所示的 Razor 代码,但我不能。 UserDetails之后没有Name属性
@Html.DisplayNameFor(model => model.ApplicationUser.UserDetails.Name)
- 为什么我在那里找不到
Select方法? - 有什么方法可以使用 include 或其他 ef 方法从
UserTests访问UserDetails属性? - 如果没有,如何将模型发送到
UserTests、ApplicationUser和UserDetails属性可访问的视图? - 如何实现显示
UserDetails.Name属性而不是ApplicationUser.Email属性?
【问题讨论】:
-
Select方法是定义为作用于IEnumerable<T>的扩展方法,ApplicationUser既不是集合也不是实现IEnumerable -
@chomba,感谢您的评论。你说的对。我已经为 ApplicationUser 实现了 IEnumerable,然后我可以访问
Select方法。但我仍然无法在视图中访问 UserDetails 的属性。
标签: c# asp.net-mvc lambda entity-framework-6 asp.net-identity-2