Understanding EF Core Relationships: Why Include public int BlogId { get; set; } and public Blog Blog { get; set; } in the Post Class?

huangapple go评论72阅读模式
英文:

Understanding EF Core Relationships: Why Include public int BlogId { get; set; } and public Blog Blog { get; set; } in the Post Class?

问题

我一直在使用Entity Framework Core,并尝试更好地理解关系。我有两个类,BlogPost,它们定义了一个一对多的关系,其中Blog包含多个Post实体。

以下是简化后的类定义:

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual Uri SiteUri { get; set; }

    public ICollection<Post> Posts { get; }
}

public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public DateTime PublishedOn { get; set; }
    public bool Archived { get; set; }

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

我的困惑来自于Post类中的以下属性:

public int BlogId { get; set; }

public Blog Blog { get; set; }

据我理解,这两个属性不会直接迁移到数据库作为列。为什么在这个关系定义中需要它们?

我考虑在Stack Overflow上提出这个问题,以澄清我的理解。是否有人能够解释包括这些属性的原因,它们在关系中的作用,以及它们如何增强Entity Framework Core的体验?

对于您的帮助,我提前表示感谢!

英文:

I have been working with Entity Framework Core and I'm trying to understand relationships better. I have two classes, Blog and Post, which define a one-to-many relationship where a Blog contains multiple Post entities.

Here are the simplified class definitions:

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual Uri SiteUri { get; set; }

    public ICollection&lt;Post&gt; Posts { get; }
}

public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public DateTime PublishedOn { get; set; }
    public bool Archived { get; set; }

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

My confusion arises from the presence of the

public int BlogId { get; set; }

and

public Blog Blog { get; set; } 

properties in the Post class. As far as I understand, these two properties are not directly migrated to the database as columns. Why are they necessary in this relationship definition?

I'm considering asking this question on Stack Overflow to clarify my understanding. Could someone provide an explanation for including these properties, their roles in the relationship, and how they enhance the Entity Framework Core experience?

Any insights would be greatly appreciated!

Thank you in advance for your help!

答案1

得分: 2

根据我理解,这两个属性并不直接迁移到数据库作为列。为什么在这个关系定义中它们是必要的?

BlogId 被添加为一个列。它将用于设置关系,并将用作外键(在关系数据库中)。实际上,指定 BlogId 是不必要的,只需添加:

public Blog Blog { get; set; }

就足够了,EF Core 将创建相应的列(但个人偏好暴露它,因为在某些情况下它很有用)。

例如,在 SQL Server 中,它应该会导致类似以下的结果:

CREATE TABLE [Posts] (
    [Id] int NOT NULL IDENTITY,
    [Title] nvarchar(max) NULL,
    [Content] nvarchar(max) NULL,
    [PublishedOn] datetime2 NOT NULL,
    [Archived] bit NOT NULL,
    [BlogId] int NOT NULL,
    CONSTRAINT [PK_Posts] PRIMARY KEY ([Id]),
    CONSTRAINT [FK_Posts_Blogs_BlogId] FOREIGN KEY ([BlogId]) REFERENCES [Blogs] ([Id]) ON DELETE CASCADE);

理论上,您可以省略这两个属性,并通过 fluent 接口设置关系,但一般来说,拥有导航属性被认为是一个良好的实践,这样您将会简化您的生活,而不是编写"手动"连接来过滤或加载相关数据。例如,以下代码:

var blogs = context.Blogs
    .Include(blog => blog.Posts)
    .ToList();

应该会生成适当的 SQL 语句,包含连接并填充所需的数据,以便于使用。

也可以参考以下资源:

英文:

> As far as I understand, these two properties are not directly migrated to the database as columns. Why are they necessary in this relationship definition?

BlogId is added as a column. It will be used to setup the relationship and will be used as foreign key (in relational database). Actually specifying BlogId is not required, just adding:

public Blog Blog { get; set; } 

Is enough, EF Core will create corresponding columns (but personally I prefer to expose it due to it being useful in some cases).

For example in SQL Server it should result in something like the following:

CREATE TABLE [Posts] (
    [Id] int NOT NULL IDENTITY,
    [Title] nvarchar(max) NULL,
    [Content] nvarchar(max) NULL,
    [PublishedOn] datetime2 NOT NULL,
    [Archived] bit NOT NULL,
    [BlogId] int NOT NULL,
    CONSTRAINT [PK_Posts] PRIMARY KEY ([Id]),
    CONSTRAINT [FK_Posts_Blogs_BlogId] FOREIGN KEY ([BlogId]) REFERENCES [Blogs] ([Id]) ON DELETE CASCADE);

> Why are they necessary in this relationship definition?

In theory you can omit both properties and setup the relationship via fluent interface, but in general having the navigation property is considered a good practice, so you will simplify your life and use them instead of writing "manual" joins to filter or load related data. For example the following:

    var blogs = context.Blogs
        .Include(blog =&gt; blog.Posts)
        .ToList();

Should generate appropriate SQL with joins and fill the needed data for the ease of use.

Sources/read also:

答案2

得分: 0

首先,您应该在 BlogId 上添加注释 [ForeignKey(nameof(this.Blog))],或者在您的上下文中的 modelCreating 中使用 Fluent API 描述关系。

通过这样做,如果您使用 Code First 方法,EF 将在数据库中创建关系。

然后,Blog 属性被称为导航属性,是告诉 EF 在两个方向上映射相关实体的方法。

因此,在您的查询中:

var posts = ctx.Post.Include(x => x.Blog).ToList();

您将会将博客与帖子关联起来。

每当您访问一个帖子对象,比如:

posts[0]

您可以通过以下方式直接访问此帖子的博客对象:

posts[0].Blogs // 这是博客的列表

这就是导航属性以及它存在的原因。

我建议您在开始编码之前阅读一本针对 EF 初学者的书籍,这样您就能更好地理解 EF 是什么以及它与数据库的关系!

英文:

First of all you should add the annotation [ForeignKey(nameof(this.Blog))
] on BlogId or describe the relation ship with fluent API on modelCreating from your context.

By doing this the ef will create the relatioship on the database if you are using the code first approach.

Then the Blog property is called as a navigation property and is the way to tell ef to map the related entity both ways.

So in your query:

Var posts = ctx.Post.Include(x => x.Blog).ToList() you will join the blog to the post.

Whenever you access a post object, lets say:

posts[0]

You will have a direct access to your blog objects of this post by doing this:

posts[0].Blogs which is a list o blogs

And thats the navigation property and the reason why is there.

I suggest that you start reading a book for ef for beginners before starting coding so you have a better understanding for what the ef it is and the relation it has with the db!!

huangapple
  • 本文由 发表于 2023年8月9日 10:03:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864134-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定