【问题标题】:Entity Framework - Use join table to return collections of Followers / Following实体框架 - 使用连接表返回关注者/关注者的集合
【发布时间】:2011-07-07 19:01:48
【问题描述】:

使用实体框架,我正在编写一个社交网络应用程序并尝试设置一些关系,以便我可以拥有具有关注者/关注者属性的用户。

我有一个用户表:

Users
_______
Id
FirstName
LastName
Email

然后我有一个关注表:

Follows
__________
FollowerId
FolloweeId

在强类型 User 对象上,我想查看返回 User 对象集合的 .Followers 和 .Following 属性。到目前为止,我已经尝试将 Follows 表中的列作为外键,并且都作为复合主键。实体模型以我想要的方式出现(我没有中间的实体,我只需要重命名导航属性),但是当我填充数据时,Followers 和 Followers 集合是空的,所以有些东西关系不对。

我想我可以分开并有两个表,Followers 和 Followers,但是当有人关注其他人时,我会有重复的数据并且必须添加到两个表中。

【问题讨论】:

  • 只需检查实体之间的关联,并确保您正在保存数据库。您的 EF 是否可以保存任何内容(关注者/关注者除外),请检查该内容。如果是,检查followers/folowees是否成功写入db。

标签: asp.net entity-framework entity-framework-4


【解决方案1】:

您需要在查询中包含与用户表有关系的表的数据。

这样的查询:

using (EntityObject context = new EntityObject())
{
    var user = from x in context.users.Include("Followers").Include("Followees") //use your tables
               where x.Id == TheUserId
               select x;


}

将允许您访问这些包含表的对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-18
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    相关资源
    最近更新 更多