0

我有一个 UserProfile 类

 public class UserProfile
{
    public UserProfile()
    {
    }

    public UserProfile(string userId)
    {
        AppUserId = userId;
    }

    [Key]
    public int UserProfileId { get; set; }

    public string AppUserId { get; set; }
    public ICollection<Blog> AuthoredBlogs { get; set; }
    public ICollection<Blog> SubscribedBlogs { get; set; }

    //other properties removed for brevity
}

以及相关的 Blog 类

 public class Blog
{
    [Key]
    public int BlogId { get; set; }

    [ForeignKey("BlogAuthor")]
    [Index("IX_AuthorIndex", 1, IsClustered = false, IsUnique = false)]
    public int AuthorId { get; set; }

    [Required]
    public Author BlogAuthor { get; set; }

    [Required(ErrorMessage = "A blog name is required")]
    public string BlogName { get; set; }

    public string BlogIconUrl { get; set; }
    public List<BlogPost> BlogPosts { get; set; }

    public EquipmentCategory EquipmentCategory { get; set; }       
    public EquipmentType EquipmentType { get; set; }

    public ICollection<int> BlogReaderIds { get; set; }

    public Blog(string name, Author author)
    {
        BlogName = name;
        BlogAuthor = author;
        EquipmentType = EquipmentType.NoSearch;
        EquipmentCategory = EquipmentCategory.NoSearch;
    }

    public Blog()
    {
        EquipmentType = EquipmentType.NoSearch;
        EquipmentCategory = EquipmentCategory.NoSearch;
    }
}

我很难弄清楚如何使用 Blog 类对 UserProfile(AuthoredBlogs 和 SubscribedBlogs)中的两个集合进行建模。在 UserProfile 中拥有这两个集合需要两个 FK 关联到博客,但我只是不明白这可以/应该如何工作。

UserProfile 可以订阅和创作许多博客。但是 Blog 类只能有一个作者和订阅的 UserProfiles 列表,或者像我在这里拥有的那样,是订阅者的 UserProfileId 列表。

我无法让它工作,由于 FK 关联问题,代码优先更新无法部署到数据库。

任何帮助表示赞赏。

4

1 回答 1

0

这些模型注释将通过自动创建的影子表在作者和博客之间创建一对多关系,以及在博客和订阅者之间创建多对多关系。

public class UserProfile
{
    //other stuff...

    [InverseProperty("Autor")]
    public ICollection<Blog> AuthoredBlogs { get; set; }
    [InverseProperty("SubscribedUserProfiles")]
    public ICollection<Blog> SubscribedBlogs { get; set; }   
}

public class Blog
{
    //other stuff..

    public ICollection<UserProfile> SubscribedUserProfiles { get; set; }        

    public UserProfile Autor { get; set; }
    [ForeignKey("Autor")]
    public int AutorId { get; set; }
}
于 2017-06-19T08:37:27.617 回答