0

I have the following ERD and want to convert it EF 4.1 Code First classes. For the sake of simplicity, I will concentrate on the first two Entities only.

Quotes Website ERD

Author > Quote.

Which is the correct way of having my code for the Entities, this:

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

Where I have given each Quote a property of type Integer signifying the IDs of the Author and Language or this:

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

Where each Quote has a property of Type Author and one of type Language.

halfer
  • 18,701
  • 13
  • 79
  • 158
J86
  • 11,751
  • 29
  • 115
  • 194

1 Answers1

1

The first example is rare. You are exposing foreign properties without using navigation properties = you are exposing database specific features but not using object oriented features.

The second example is common and there is even third possible way where your Quote entity will have both FKs and navigation properties. That will make difference between independent and foreign key associations.

Community
  • 1
  • 1
Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654