【问题标题】:When I try navigating to third level entity properties, I can't find Select method with lambda expression inside Include method当我尝试导航到第三级实体属性时,我在 Include 方法中找不到带有 lambda 表达式的 Select 方法
【发布时间】: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)
  1. 为什么我在那里找不到Select 方法?
  2. 有什么方法可以使用 include 或其他 ef 方法从 UserTests 访问 UserDetails 属性?
  3. 如果没有,如何将模型发送到UserTestsApplicationUserUserDetails 属性可访问的视图?
  4. 如何实现显示UserDetails.Name 属性而不是ApplicationUser.Email 属性?

【问题讨论】:

  • Select 方法是定义为作用于IEnumerable&lt;T&gt; 的扩展方法,ApplicationUser 既不是集合也不是实现IEnumerable
  • @chomba,感谢您的评论。你说的对。我已经为 ApplicationUser 实现了 IEnumerable,然后我可以访问 Select 方法。但我仍然无法在视图中访问 UserDetails 的属性。

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


【解决方案1】:

这是您要查找的Include

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

【讨论】:

  • 感谢您的回答。我以前尝试过,但它对我没有用。当我将此发送到视图时,我无法访问 UserDetails.Name。
  • UserDetails.Name 不存在。您应该显示名称foreachUserDetail。
  • 我可以通过DisplayFor 区域中的嵌套foreach 循环访问UserDetail 属性。但是在DisplayNameFor 区域做什么呢?我应该只写字符串还是用 ViewBag 发送UserDetails.Name?我可以在那里将名称显示为强类型吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-25
  • 2020-03-12
  • 2013-11-14
  • 1970-01-01
  • 2011-11-23
相关资源
最近更新 更多