1

现在我正在学习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; }
    }

首先,我认为使用 ofList<>是实现类似外键的行为的方式,但现在知道这不是我们需要的原因以及我们在实体中使用List<>的目的是什么?

4

1 回答 1

1

为了表明博客有很多帖子,当您在 DB 中构建项目时,将是关系 1xBlog--->NxPost,其中 N=unlimited。这将表明每个人都Blog可以拥有无​​限数量的Posts

于 2013-02-14T13:12:42.907 回答