ERD到Code First Classes

时间:2011-09-26 08:53:45

标签: entity-framework-4.1 ef-code-first

我有以下ERD并希望将其转换为EF 4.1 Code First类。为简单起见,我将只专注于前两个实体。

Quotes Website ERD

作者>报价。

使用实体代码的正确方法是哪种,

public class Quote
{
    public int Id { get; set; }
    [Required, MaxLength(500)]
    public string Body { get; set; }
    public int Likes { get; set; }
    [Required]
    public bool isApproved { get; set; }
    [Required]
    public DateTime CreatedOn { get; set; }
    public int AuthorId { get; set; }
    public int LanguageId { get; set; }
    public virtual ICollection<Tag> Tags { get; set; }
}

public class Author
{
    public int Id { get; set; }
    [Required, MaxLength(30), MinLength(2)]
    public string FirstName { get; set; }
    [Required, MaxLength(30), MinLength(2)]
    public string LastName { get; set; }
    [Required]
    public DateTime DOB { get; set; }
    public DateTime DOD { get; set; }
    [Required, MaxLength(60), MinLength(2)]
    public string Occupation { get; set; }
    [Required, MaxLength(170), MinLength(5)]
    public string WikiLink { get; set; }
    public byte[] Image { get; set; }
    [Required]
    public bool isApproved { get; set; }
    [Required]
    public DateTime CreatedOn { get; set; }
    public virtual ICollection<Quote> Quotes { get; set; }
}

我给每个引用一个Integer类型的属性,表示作者和语言的ID 或者

public class Quote
{
    public int Id { get; set; }
    [Required, MaxLength(500)]
    public string Body { get; set; }
    public int Likes { get; set; }
    [Required]
    public bool isApproved { get; set; }
    [Required]
    public DateTime CreatedOn { get; set; }
    public Author Author { get; set; }
    public Language Language { get; set; }
    public virtual ICollection<Tag> Tags { get; set; }
}

public class Author
{
    public int Id { get; set; }
    [Required, MaxLength(30), MinLength(2)]
    public string FirstName { get; set; }
    [Required, MaxLength(30), MinLength(2)]
    public string LastName { get; set; }
    [Required]
    public DateTime DOB { get; set; }
    public DateTime DOD { get; set; }
    [Required, MaxLength(60), MinLength(2)]
    public string Occupation { get; set; }
    [Required, MaxLength(170), MinLength(5)]
    public string WikiLink { get; set; }
    public byte[] Image { get; set; }
    [Required]
    public bool isApproved { get; set; }
    [Required]
    public DateTime CreatedOn { get; set; }
    public virtual ICollection<Quote> Quotes { get; set; }
}

其中每个Quote都有Type Author属性和一个Language类型。

我非常感谢任何帮助。

谢谢。

1 个答案:

答案 0 :(得分:1)

第一个例子很少见。您在不使用导航属性的情况下公开外部属性=您正在公开数据库特定功能但不使用面向对象的功能。

第二个例子很常见,甚至还有第三种方法,你的Quote实体将同时具有FK和导航属性。这将使difference between independent and foreign key associations

相关问题