2011-09-26 27 views
0

我有以下ERD,並希望將其轉換爲EF 4.1 Code First類。爲了簡單起見,我只專注於前兩個實體。ERD代碼第一類

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

在哪裏我已經給每個引號整數類型標誌着作者和語言或本的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; } 
} 

每個地方都有引用類型作者和類型的語言之一的財產。

我將不勝感激任何幫助。

謝謝。

回答

1

第一個例子很少見。您在不使用導航屬性的情況下公開外部屬性=您正在公開數據庫特定功能,但未使用面向對象功能。

第二個例子很常見,甚至有第三種可能性,您的報價實體將同時具有FK和導航屬性。這將使difference between independent and foreign key associations

+0

謝謝拉迪斯拉夫,所以你會推薦我使用?第二個例子?還是第三個你連接我?謝謝 – Ciwan

+0

我喜歡第二個,因爲它更面向對象,但第三個有時更易於使用。 –

+0

第二個。 **非常感謝**。 – Ciwan