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

时间:2015-04-16 21:43:44

标签: c# asp.net-mvc lambda entity-framework-6 asp.net-identity-2

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

我使用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)
  1. 为什么我找不到Select方法?
  2. 有没有办法通过include或其他ef方法从UserDetails访问UserTests属性?
  3. 如果没有,如何将模型发送到UserTestsApplicationUserUserDetails属性可以访问的视图?
  4. 如何才能显示UserDetails.Name属性而不是ApplicationUser.Email属性?

1 个答案:

答案 0 :(得分:0)

这是您正在寻找的Include

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