简体   繁体   English

首先使用EF代码创建数据库和表

[英]Create db and tables with EF Code First

I am going to create a database and tables with EF Code First. 我将使用EF Code First创建数据库和表。

EF数据库

Each Post belongs to a single Category and it can be labelled with many Tags. 每个帖子都属于一个类别,可以用许多标签标记。 Between Post and Category the relationship is many-to-one and between Post and Tag the relationship is many-to-many. Post和Category之间的关系是多对一的,Post和Tag之间的关系是多对多的。

Below is my code: 下面是我的代码:

public class BlogContext : DbContext
{
    public DbSet<Post> Posts { get; set; }
    public DbSet<PostTagMap> PostTags { get; set; }
    public DbSet<Category> Categorys { get; set; }
    public DbSet<Tag> PostTags { get; set; }
}
public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string ShortDescription { get; set; }
    public string Meta { get; set; }
    public string UrlSlug { get; set; }
    public bool Published { get; set; }
    public DateTime PostedOn { get; set; }
    public DateTime? Modified { get; set; }
    public Category Category { get; set; }
    public IList<Tag> Tags { get; set; }
}
public class PostTagMap
{

}
public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string UrlSlug { get; set; }
    public string Description { get; set; }
    public IList<Post> Posts { get; set; }
}
public class Tag
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string UrlSlug { get; set; }
    public string Description { get; set; }
    public IList<Post> Posts { get; set; }
}

Here are the EF new guy's questions: 以下是EF新手的问题:

  1. Do I generate code for PostTagMap ? 我是否为PostTagMap生成代码?
  2. How to apply relationship between tables to my code? 如何将表之间的关系应用于我的代码?
  3. I always saw that people add DataAnnotation at this moment? 我总是看到人们在这一刻添加DataAnnotation? Because many online samples created DB without this step, I am confused. 因为许多在线样本没有执行此步骤就创建了数据库,所以我很困惑。

Your data model looks fine. 您的数据模型看起来不错。 I would use that. 我会用。 For your questions: 对于您的问题:

Do I generate code for PostTagMap? 是否为PostTagMap生成代码? what kind of code you want to generate? 您想生成什么样的代码? If you mean, do u define any code for it. 如果您的意思是,您是否定义了任何代码。 you dont really need to since you already have a relationship defined between post and tag. 您实际上不需要,因为您已经在post和tag之间定义了关系。 But you can if you think it will be easier for you to query. 但是,如果您认为查询起来会更容易,则可以。

How to apply relationship between tables to my code? 如何将表之间的关系应用于我的代码? The relationship is already there. 关系已经存在。 Such as Category has many posts and a post belongs to a category. 如类别有很多帖子,而帖子属于一个类别。

I always saw that people add DataAnnotation at this moment? 我总是看到人们在这一刻添加DataAnnotation? Because many online samples created DB without this step, I am confused. 因为许多在线样本没有执行此步骤就创建了数据库,所以我很困惑。 Annotaions are used as cross cutting concerns such as validation, data type, data length and so forth. 注释被用作横切关注点,例如验证,数据类型,数据长度等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM