开发者

How to convert an ERD to Code First classes?

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.

How to convert an ERD to Code First classes?

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开发者_JAVA百科 { 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜