实体框架中的关注者架构

时间:2015-07-19 08:21:27

标签: c# asp.net-mvc entity-framework ef-fluent-api

我正在制作一款具有以下功能的应用程序。但我被困在这里。我是这样来的。

public class User
{
  public int Id { get; set; }
  public ICollection<User> Followers { get; set; }
  public ICollection<User> Following { get; set; }
}

关注者表将是

public class Followers
{
public int UserId { get; set; }
public int FollowerId { get; set; }
}

但是在这种方法中,实体框架将如何知道哪些是追随者以及我追随哪些? 在此先感谢。

3 个答案:

答案 0 :(得分:1)

您希望首先使用代码获得多对多的自引用关系吗?你不需要Followers课程。只需定义User类:

public class User
{
  public int Id { get; set; }
  public ICollection<User> Followers { get; set; }
  public ICollection<User> Following { get; set; }
}

然后以这种方式编写DbContext课程:

public class MyEntities: DbContext
{
  public DbSet<User> Users { get; set; }

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Entity<User>()
        .HasMany(x => x.Followers).WithMany(x => x.Following)
        .Map(x => x.ToTable("Followers")
            .MapLeftKey("UserId")
            .MapRightKey("FollowerId"));
  }
}

将在数据库中创建Followers表以及Users表。您可以使用以下代码对其进行测试:

using (var db = new MyEntities()) {
  var user1 = new User();
  var user2 = new User();
  var user3 = new User();
  user1.Followers = new User[] { user2 };
  user2.Followers = new User[] { user3 };
  db.Users.Add(user1);
  db.SaveChanges();
}

更新

实体框架如何知道哪些是关注者以及哪些关注者?答案是,它以这种方式解释modelBuilder语句:

  • Entity<User>:主题为User;
  • HasMany(x => x.Followers):用户有很多关注者;
  • WithMany(x => x.Following):每个粉丝都有很多关注者;
  • Map(...):链接表为Followers,左键指向主题(用户),右键指向主题相关实体(关注者)。

答案 1 :(得分:0)

根据您的要求,您可以执行以下操作:

public class User
{
  public int Id { get; set; }
  public ICollection<Follower> Followers { get; set; }
  public ICollection<Follower> Following { get; set; }
}

public enum EFollowerType
{
    Follower=1,
    Following
}

public class Follower
{
  public int Id { get;  set; }
  public int UserId { get; set; }
  public int FollowedById { get; set; }
  public EFollowerType FollowerType { get; set; }

  public User User { get; set; }
  public User FollowedBy { get; set; }
}

//in the OnModelCreating function in the DbContext 

modelBuilder.Entity<Follower>()
            .HasRequired(m=> m.User)
            .WithMany(m=> m.Followers)
            .HasForeignKey(k=> k.UserId);
modelBuilder.Entity<Follower>()
            .HasRequired(m=> m.FollowedBy)
            .WithMany(m=> m.Following)
            .HasForeignKey(k=> k.FollowedById);

//to add a follower 
var user = ... user details goes here ... 
var follower = new Follower(){
   FollowerType = FollowerType.Follower, // if you want it as Follower
   User = user,
};
db.Followers.Add(follower);
db.SaveChanges();

// to know how many followers the user has

var followers = user.Followers.Count(t=>t.FollowerType ==EFollowerType.Follower);

// to know how many the user is following 
var following = user.Followers.Count(t=>t.FollowerType ==EFollowerType.Following);

如果您想要制作多语言数据库,或者想要在不修改代码的情况下添加其他类型的关注者(枚举),您也可以将FollowerType作为表格

希望这会对你有所帮助

答案 2 :(得分:0)

您可以简单地使用:

modelBuilder.Entity<User>().HasMany(m => m.Followers).WithMany(m=>m.Following ).Map(x => x.MapLeftKey("Id").MapRightKey("FollowerId").ToTable("Followers"));