许多到自己加入实体框架代码第一 [英] Many to Many self Join with Entity Framework Code First
问题描述
考虑这个 Poco
:
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public string Fullname { get; set; }
}
现在我想实现一个跟随技术,用户可以跟随其他用户所以基本上它的自己很多到很多关系
Now i want to implement a follow technique where a user may follow other users so basically its self Many to Many relationship
问题是我不知道我可以在实体框架
代码优先
?
problem is i don't know how exactly i can achieve this in Entity Framework
Code-First
?
我想到了一个链接器表:
I thought of a linker Table :
public class UserFollow
{
public int Id { get; set; }
public int Follower { get; set; }
public int Following { get; set; }
public DateTime FollowDate { get; set; }
}
我希望能够从每个用户对象获取所有关注者和关注?
i want to be able to get All Followers and Following from every User Object?
推荐答案
这是非常简单的,使用EF代码优先,因为您只需要用户POCO:
This is quite simple using EF code-first as you only need the User POCO:
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public string Fullname { get; set; }
public ICollection<User> FollowedUsers { get; set; }
}
集合意味着用户
与其他用户有关。
The collection means that a User
is related to other users.
PS:我注意到您在解决方案示例中添加了时间戳。为了实现这一点,你应该添加更改通用类型的集合,以满足您的需要。
PS: I noted you added a timestamp in your solution example. To achieve that you should still add the collection changing the generic type to whatever suits your needs.
希望它有帮助。
这篇关于许多到自己加入实体框架代码第一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!