ADO.NET EF使用List<>的目的是什么?在实体中

时间:2013-02-14 13:06:50

标签: code-first ado.net-entity-data-model

现在我正在学习ADO.NET Entity Framework并且有一件事我无法向自己解释。以下是我最近使用过的教程的源代码:

public class Blog
    {
        public int BlogId { get; set; }
        public string Name { get; set; }
        public string Url { get; set; }
        public User UserId { get; set; }

        public virtual List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }

        public int BlogId { get; set; }
        public virtual Blog Blog { get; set; }
    }

    public class User
    {
        public int UserId { get; set; }
        public string Username { get; set; }
        public string DisplayName { get; set; }
    }

首先,我认为使用List<>是实现外键式行为的方式,但现在知道我们不需要为什么以及为什么使用List<>在我们的成员中?

1 个答案:

答案 0 :(得分:1)

要显示博客有很多帖子,当您在DB中构建项目时,将关系1xBlog ---&gt; NxPost,其中N =无限制。这将表明每个Blog可以拥有无​​限量的Post s