实体框架查询返回一条记录,但它应该返回更多

时间:2012-06-12 13:00:55

标签: entity-framework entity-relationship dbcontext

正如标题所说,我在Asp.net mvc 3 EF中遇到了查询问题。 我有2张桌子,有1对多的关系。

table1 Users int user_ID string username

table2 Friends int friendshipID int user_ID int friend_ID

控制器:

// // GET:/ User / Details / 5

public ViewResult Details(int id)
    {
        User user = db.Users.Include("Friends").FirstOrDefault(u => u.user_ID == id);
        //Also for each friend get the User:
        foreach (var friend in user.Friends.ToList())
        {
            friend.User = db.Users.Find(friend.friend_ID);
        }
        return View(user);
    }

视图“详情”:

@model Social2.Models.User

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>
    <div class="display-field">
        @foreach (var friend in @Model.Friends)
    {
           @friend.User.username;
    }   
    </div>

上下文:

public partial class ASPNETDBEntities : DbContext
{
    public ASPNETDBEntities()
        : base("name=ASPNETDBEntities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public DbSet<aspnet_Users> aspnet_Users { get; set; }
    public DbSet<Friend> Friends { get; set; }
    public DbSet<User> Users { get; set; }
}

用户模型:

public partial class User
{
    public User()
    {
        this.Friends = new HashSet<Friend>();
    }
    [Key]
    public int user_ID { get; set; }
    public System.Guid user_UniqueID { get; set; }
    public string username { get; set; }

    public virtual aspnet_Users aspnet_Users { get; set; }
    public virtual ICollection<Friend> Friends { get; set; }
}

朋友模特

public partial class Friend
{
    public int friendship_ID { get; set; }
    public int user_fr_ID { get; set; }
    public int friend_ID { get; set; }

    public virtual User User { get; set; }
}

问题是,当我去〜/ user / details / 1时,视图只显示一个(最后一个)朋友。对于每个用户,它显示他们的最后一个朋友。如何展示它们?

3 个答案:

答案 0 :(得分:0)

我认为问题出在这里

User user = db.Users.Include("Friends").FirstOrDefault(u => u.user_ID == id);

FirstOrDefault只提供一条记录。

答案 1 :(得分:0)

我认为问题在于朋友类的映射。

尝试更改为:

public partial class Friend
{
    [Key]
    public int friendship_ID { get; set; }
    public int user_fr_ID { get; set; }
    public int friend_ID { get; set; }

    [ForeignKey("friend_ID")]
    public virtual User User { get; set; }
}

答案 2 :(得分:0)

您的数据库必须具有两个关系,如下所示:

DB Schema

如果您从此架构创建实体模型,您还会获得两个一对多关系。当你将DBContext T4模板应用于这个模型时,你应该得到像这样的POCO类:

public partial class Users
{
    public Users()
    {
        this.Friends = new HashSet<Friends>();
        this.Friends1 = new HashSet<Friends>();
    }

    public int user_ID { get; set; }
    public string username { get; set; }

    public virtual ICollection<Friends> Friends { get; set; }
    public virtual ICollection<Friends> Friends1 { get; set; }
}

public partial class Friends
{
    public int friendship_ID { get; set; }
    public int user_fr_ID { get; set; }
    public int friend_ID { get; set; }

    public virtual Users Users { get; set; }
    public virtual Users Users1 { get; set; }
}

Users.FriendsFriends.Users为第一个关系形成一对,Users.Friends1Friends.Users1是第二个关系的一对。 (您可以在模型设计器中重命名导航属性以使名称更有意义。)您的查询将如下所示(您可以包含“第二级”并且不需要像在示例中那样执行循环):

public ViewResult Details(int id)
{
    // important to use User1 in Include, not User
    User user = db.Users.Include("Friends.User1")
        .FirstOrDefault(u => u.user_ID == id);
    return View(user);
}

使用DbContext您还可以使用强类型版Include

Include(u => u.Friends.Select(f => f.User1))