实体框架定义复杂的对象关系

时间:2016-04-13 19:52:24

标签: c# asp.net .net entity-framework

我有用户关注类定义如下:

public class User : IdentityUser
{
    public User()
    {
        Followers = new HashSet<Following>();
        Following = new HashSet<Following>();
    }

    [Key]
    public string ID { get; set; }
...
...

    public virtual ICollection<Following> Followers { get; set; }

    public virtual ICollection<Following> Following { get; set; } 
}

public class Following
{
        [Key]        
        public int ID {get;set;}

        [MaxLength(100)]
        public string follower { get; set; }

        [MaxLength(100)]
        public string following { get; set; }           

        public virtual User UserFollower { get; set; }

        public virtual User UserFollowing { get; set; }
 }

以下中的属性关注者和跟随者都是User对象的外键并表示关系。然后在DBcontext类中,我重写了OnModelCreating:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {      
        modelBuilder.Entity<User>()
            .HasMany<Following>(u => u.Followers)
            .WithOptional()
            .HasForeignKey(c => c.following);

        modelBuilder.Entity<User>()
            .HasMany<Following>(u => u.Following)
            .WithOptional()
            .HasForeignKey(c => c.follower);
}

这非常适合吸引用户的关注者和关注者:

User user = await UserManager.FindByIdAsync(ID);
List<Following> followers = user.Following.ToList();

我的基本问题是如何创建UserFollower&amp; UserFollowing关系,以便以下的每个属性都映射到用户?

理想情况下,我可以按照以下方式做一些事情:

User user = await UserManager.FindByIdAsync(ID);
List<Following> followers = user.Following.ToList();

User userFollowing = followers[0].UserFollowing;

1 个答案:

答案 0 :(得分:1)

稍微修改你的模型

public class User : IdentityUser
{
    public User()
    {
        // Don't do this. Entity Framwork will do that for you
        // Followers = new HashSet<Following>();
        // Following = new HashSet<Following>();
    }

    // Don't add this. IdentityUser has already defined it as `Id`
    // [Key]
    // public string ID { get; set; }

    public virtual ICollection<Following> Followers { get; set; }

    public virtual ICollection<Following> Following { get; set; } 
}

public class Following
{
    [Key]        
    public int ID {get;set;}

    // [MaxLength(100)] No need to do this as this is Foreign Key Property
    public string follower { get; set; }

    // [MaxLength(100)] No need to do this as this is Foreign Key Property
    public string following { get; set; }           

    public virtual User UserFollower { get; set; }

    public virtual User UserFollowing { get; set; }
}

然后用户流畅的API来定义关系

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{      
    modelBuilder.Entity<User>()
        .HasMany(u => u.Followers)
        .WithRequired(f => f.UserFollower)
        .HasForeignKey(c => c.following);

    modelBuilder.Entity<User>()
        .HasMany(u => u.Following)
        .WithRequired(f => f.UserFollowing)
        .HasForeignKey(c => c.follower);
}

这将创建双向关系。进一步的EntityFramework将覆盖所有虚拟属性并定义您期望的行为。

如果您尚未修改EntityFramework配置,则可以直接执行此操作

User user = await UserManager.FindByIdAsync(ID);
List<Following> followers = user.Following.ToList();

User userFollowing = followers[0].UserFollowing;