2011-09-20 16 views
0

表1:文章表示一个帮助台在实体框架

表2:ArticleCategories

我怎样表示它是1-> 1的关系的两个表之间的关系:

我可以做到以下几点,但我不知道这是正确的做法:

public class Article 
{ 
    public int ArticleIndex { get; set; } 
    public int Category { get; set; } 
    public Guid User { get; set; } 
    public int Parent { get; set; } 
    public int Level { get; set; } 
    public int Order { get; set; } 
    public DateTime DateCreated { get; set; } 
    public DateTime DateExpires { get; set; } 
    public bool Show { get; set; } 
    public string Title { get; set; } 
    public string TitleHtml { get; set; } 
    public string Content { get; set; } 
    public string ContentHtml { get; set; } 
    public string ShortTitle { get; set; } 
    public ArticleCategory Category { get; set; } 
} 

public class ArticleCategory 
{ 
    public int CategoryIndex { get; set; } 
    public string Name { get; set; } 
} 
+0

您是否使用代码第一次? – devuxer

+0

是............................................ – user560498

+1

不要你的意思是一个1->多种关系? *一个*类别有*许多*文章和一篇文章属于*一个*类别? – Slauma

回答

0

按照惯例,代码第一次预计的Id属性为每个类/表。然后你可以这样做:

public class Article 
{ 
    public int Id { get; set; } 
    public int ArticleIndex { get; set; } 
    public int Category { get; set; } 
    public Guid User { get; set; } 
    public int Parent { get; set; } 
    public int Level { get; set; } 
    public int Order { get; set; } 
    public DateTime DateCreated { get; set; } 
    public DateTime DateExpires { get; set; } 
    public bool Show { get; set; } 
    public string Title { get; set; } 
    public string TitleHtml { get; set; } 
    public string Content { get; set; } 
    public string ContentHtml { get; set; } 
    public string ShortTitle { get; set; } 
    public int ArticleCategoryId { get; set; } 

    public virtual ArticleCategory ArticleCategory { get; set; } 
} 

public class ArticleCategory 
{ 
    public int Id { get; set; } 
    public int CategoryIndex { get; set; } 
    public string Name { get; set; } 

    public virtual ICollection<Article> Articles { get; set; } 
} 

请注意virtual关键字。 EF Code First需要这样才能在幕后执行其魔法。现在

,如果您正在使用Article工作,你可以通过做article.ArticleCategory得到它的所有类别的信息,如果你有一个ArticleCategory,你可以发现它是指用articleCategory.Articles.Single()什么文章。

欲了解更多信息,请参见本文由斯科特谷:

http://weblogs.asp.net/scottgu/archive/2010/12/08/announcing-entity-framework-code-first-ctp5-release.aspx