I have a UserProfile class
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
}
And the associated Blog class
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;
}
}
I am having a hard time figuring out how to model the two collections in UserProfile (AuthoredBlogs and SubscribedBlogs) with Blog class. Having those two collections in UserProfile would require two FK associations to Blog but I just dont see how that can/should work.
A UserProfile can subscribe to and author many Blogs. But the Blog class can only have one author and either a list of subscribed UserProfiles or , as I have it here, a list of the subscriber's UserProfileId's.
I cant get it to work, the code first updates are failing to deploy to the db due to the FK association issues.
Any help appreciated.
These models annotations will create one-to-many relation between autor and blogs and many-to-many relation between blogs and subscribers via autocreated, shadow table.
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; }
}