0

正如标题所说,我在 Asp.net mvc 3 EF 中的查询有问题。我得到了 2 个具有 1 对多关系的表。

table1 用户 int user_ID string 用户名

table2 Friends int FriendshipID int user_ID intfriend_ID

控制器:

// // 获取:/用户/详细信息/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 时,视图只显示一个(最后一个)朋友。对于每个用户,它都会显示他们的最后一个朋友。如何全部展示?

4

3 回答 3

0

我认为问题出在这里

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

FirstOrDefault只给出一条记录。

于 2012-06-12T13:10:06.333 回答
0

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

尝试更改为:

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; }
}
于 2012-06-12T13:26:54.100 回答
0

你的数据库必须有两个这样定义的关系:

数据库架构

如果您从此模式创建实体模型,您还将获得两个一对多关系。当您将 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))
于 2012-06-12T14:42:51.943 回答