在一个专业的社交网络中,我如何表示用户之间的联系?(就像Linkedin),我是否应该创建一个连接类,其中每个用户之间的连接都有一个实例,或者这是多余的?用户类是否应该有自关联(自反关联)?
发布于 2022-04-28 06:56:58
您的User类将包含以下内容:
public class User
{
// ... the other code is omitted for the brevity
public IEnumerable<User> Followings { get; set; }
}因此,如果您的数据库有Following表:
CREATE TABLE Followings(
User_Id INT NOT NULL,
Following_Id INT NOT NULL,
DateCreated DATE NOT NULL,
);不要忘记在表中创建约束和外键。那么就有可能拥有Following类:
public class Followings {
public UserId int { get; set; }
public FollowingId int { get; set; }
public DateCreated DateTime { get; set; }
}然后您可以轻松地编写以下查询:
select * from Following where UserId = x.id -- or vice versahttps://stackoverflow.com/questions/72037787
复制相似问题