2017-01-05 16 views
0

我想帶上屬於個人資料或頁面的帖子。兩班同班,一對多關係

我怎樣才能涉及到兩類以外的同一類。

你推薦什麼樣的安排?

請幫幫我。

class Profile 
    { 
     public int ID { get; set; } 

     public string Name { get; set; } 

     public List<Post> Posts { get; set; } 
    } 


class Page 
    { 
     public int ID { get; set; } 

     public string Name { get; set; } 

     public List<Post> Posts { get; set; } 
    } 

class Post 
{ 
    public int ID { get; set; } 
    public string Description { get; set; }   
    public ProfileOrPage MyProperty { get; set; } //<<< My Problem Property 
} 

public void GetProfilePost() 
     { 
      List<Post> postList = new List<Post>(); 

      postList.Add(new Post 
      { 
       ID = 1, 
       new ProfileOrPage {  // My Problem Property 
       ID = 3 
       } 
      }); 

      Profile p = new Profile(); 
      p.Posts = postList; 


     } 
+1

添加您的上下文。什麼是「我的問題屬性」? –

回答

0

您可以使用繼承。

class AbstractPage 
{ 
    public int ID { get; set; } 

    public string Name { get; set; } 

    public List<Post> Posts { get; set; } 
} 
class Profile : AbstractPage{} 
class Page : AbstractPage{} 
class Post 
{ 
    public int ID { get; set; } 
    public string Description { get; set; }   
    public AbstractPage MyProperty { get; set; } 
}