2017-06-08 57 views
0

我有一個用戶配置類域類建模衝突

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 
} 

而且

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; 
    } 
} 

我有一個很難搞清楚如何在用戶配置兩個集合(AuthoredBlogs模型和相關博客類SubscribedBlogs)和Blog類。在UserProfile中擁有這兩個集合需要兩個FK關聯到Blog,但我不知道該如何/應該如何工作。

UserProfile可以訂閱並創作許多博客。但是Blog類只能有一個作者和一個訂閱的UserProfiles列表,或者像我這裏的那樣,有一個訂閱者的UserProfileId列表。

我不能得到它的工作,由於FK協會問題代碼第一次更新未能部署到數據庫。

任何幫助表示讚賞。

回答

0

這些模型註釋將通過自動創建在創作者和博客以及博客和訂閱者之間創建一對多關係,shadow表。

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; } 
} 
+0

...虐待它,看看 – dinotom