如何访问由EF构建的相关数据 [英] How to access Related Data built By the EF

查看:147
本文介绍了如何访问由EF构建的相关数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于表 UserInRoles UserProfiles 角色 c $ c>

Similar to the simple Membership UserProfiles to Roles in the Table UserInRoles

我已经在 UserProfiles 客户端在表 UserInClients 中使用此代码

I have created a Relationship between UserProfiles to Clients in the table UserInClients with this code

modelBuilder.Entity<UserProfiles>()
            .HasMany<dbClient>(r => r.Clients)
            .WithMany(u => u.UserProfiles)
            .Map(m =>
            {
                m.ToTable("webpages_UsersInClients");
                m.MapLeftKey("ClientId");
                m.MapRightKey("UserId");
            });

我的 UserProfiles 有一个 public virtual ICollection< dbClient>客户{get;组; } 和我的客户端有一个 public virtual ICollection< UserProfiles> UserProfiles {get;组;

My UserProfiles has an public virtual ICollection<dbClient> Clients { get; set; } and my Clients has an public virtual ICollection<UserProfiles> UserProfiles { get; set; }


  • 如果您需要看到模型让我知道我可以发布他们

在模型和视图中,我想


  1. 显示全部

  2. 创建一个只显示客户端的视图目前登录的用户被允许查看。

我以为这样访问我的模型的属性Clients.ClientID一样简单,我一直在尝试像Clients.ClientID.select(u => u.UserId == clientid),但我知道更好,知道它不会,也不会工作。

I thought it was as easy as accessing the Properties of my models Clients.ClientID, i have been trying things like Clients.ClientID.select(u=>u.UserId == clientid) but i knew better and know it does not and will not work.

我的其他想法是在其中创建一个带有CliendID和UserID的模型(如表它创建),所以我可以使用我的控制器中的加入来找到正确的值?

My other thoughts were creating a model with CliendID and UserID in it ( like the table it created ) so i can use a join in my controller to find the right values??

到底是什么想要获得的是在我的 GetCascadeCl中填入一个 KendoUI CascadingDropDownList 客户端 JsonResult

In the end what i'm trying to accomlish is to populate a KendoUI CascadingDropDownList with this line in my GetCascadeClients JsonResult

return Json(db.Clients.Select(c => new { ClientID = c.ClientID, ClientName = c.Client }), JsonRequestBehavior.AllowGet);

我的问题是,当我在我的控制器中时,如何访问由Entity框架?

My question is, when I'm in my Controller, how do I access this table built by Entity Framework?

编辑:


解决方案QUERY 答案



  return Json(db.Clients
              .Include(c => c.UserProfiles)
              .Where(c => c.UserProfiles.`Any(up => up.UserName == User.Identity.Name))`
              .Select(c => new
              {
                  ClientID = c.ClientID,
                  ClientName = c.Client,
                  UserCount = c.UserProfiles.Count()
              }),
              JsonRequestBehavior.AllowGet);


推荐答案

这更像是一个LINQ问题: p>

This is more of a LINQ question really:

db.Clients
    .Where(c => c.UserProfiles.Any(up => up.UserId == loggedInUserId))
    .Select(c => new {
        ClientId = c.ClientID,
        ClientName = c.Client + " (" + c.UserProfiles.Count() + ")"
    })

由于它会将上述转换为SQL调用,我不得不使用字符串连接,好像您尝试使用一个不错的 String.Format({0}({1}),c.Client,c.UserProfiles.Count())它会抱怨无法将其转换为SQL。

Due to the fact that it will convert the above into SQL calls, I had to use string concatenation, as if you try to use a nice String.Format("{0} ({1})", c.Client, c.UserProfiles.Count()) it will complain about being unable to translate that to SQL.

其他选项是进行2遍查询,在执行数据之前实现额外的格式:

The other options is to do a 2-pass query, materializing the data before doing extra formatting:

db.Clients
    .Where(c => c.UserProfiles.Any(up => up.UserId == loggedInUserId))
    .Select(c => new {
        ClientId = c.ClientID,
        ClientName = c.Client,
        ProfileCount = c.UserProfiles.Count()
    })
    // this forces SQL to execute
    .ToList()
    // now we're working on an in-memory list
    .Select(anon => new {
        anon.ClientId,
        ClientName = String.Format("{0} ({1})", anon.ClientName, anon.ProfileCount)
    })

这篇关于如何访问由EF构建的相关数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆