当我尝试导航到第三级实体属性时,在 Include 方法中找不到带有 lambda 表达式的 Select 方法

When I try navigating to third level entity properties, I can't find Select method with lambda expression inside Include method

我在访问上下文中的第三级导航属性时遇到问题。我已经搜索了两天但是 找不到任何接近我的问题。所以我认为我的方法有逻辑错误。

我使用的是MVC模板项目。模型 classes 和上下文如下。

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 模型 class 中的电子邮件。所以我想输入如下所示的 Razor 代码,但我不能。 UserDetails

后没有Name属性
@Html.DisplayNameFor(model => model.ApplicationUser.UserDetails.Name)
  1. 为什么我在那里找不到 Select 方法?
  2. 是否有任何方法可以使用 include 或其他 ef 方法从 UserTests 访问 UserDetails 属性?
  3. 如果没有,如何将模型发送到 UserTestsApplicationUserUserDetails 属性可以访问的视图?
  4. 如何实现显示 UserDetails.Name 属性 而不是 ApplicationUser.Email 属性?

这是您正在寻找的Include

db.UserTests.Include(x => x.ApplicationUser.UserDetails);