我在上下文中访问第三级导航属性时遇到问题。我已经搜了好两天但是 在我的附近找不到任何问题。所以我认为我的方法存在逻辑错误。
我使用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; }
}
我尝试从UserDetails
获取UserTests
个属性。但我无法在Select
方法中找到Include
方法点击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代码,但我不能。 Name
UserDetails
属性
@Html.DisplayNameFor(model => model.ApplicationUser.UserDetails.Name)
Select
方法?UserDetails
访问UserTests
属性?UserTests
,ApplicationUser
和UserDetails
属性可以访问的视图?UserDetails.Name
属性而不是ApplicationUser.Email
属性?答案 0 :(得分:0)
这是您正在寻找的Include
:
db.UserTests.Include(x => x.ApplicationUser.UserDetails);