简体   繁体   English

使用Entity Framework Core以多对多关系为数据库播种

[英]Seeding the database with a many-to-many relationship using Entity Framework Core

I created a many-to-many relationship with Entity Framework Core using this example but have a few questions: 我使用此示例与Entity Framework Core建立了多对多关系,但有几个问题:

Would it hurt anything if I add a DbSet BlogPosts to the DbContext even though they don't do it in the example? 即使我在示例中没有将DbSet BlogPosts添加到DbContext,也会对它造成任何伤害吗?

If I don't add the DbSet to the DbContext what would be the proper way to create a Blog and then add multiple Posts to it when seeding the database. 如果我不将DbSet添加到DbContext,那么在创建数据库时,创建博客然后在其中添加多个帖子的正确方法是什么。 Something about the below just seems so strange. 以下内容似乎很奇怪。 I would prefer simply creating BlogPost objects and adding them to a BlogPosts in the context. 我希望仅创建BlogPost对象并将它们添加到上下文中的BlogPosts中。

var blog1 = new Blog() { Name = "ABC" };
blog1.BlogPosts.Add(new BlogPost { BlogId = blog1.Id, PostId = post1.Id });
var blog2 = new Blog() { Name = "123" };
blog2.BlogPosts.Add(new BlogPost { BlogId = blog2.Id, PostId = post1.Id });
var blog3 = new Blog() { Name = "XYZ" };
blog3.BlogPosts.Add(new BlogPost { BlogId = blog3.Id, PostId = post2.Id });
dbContext.SaveChanges();

Also, is it mandatory that I add a List navigation property to both the Blog and Post like in the example if I will only care about querying them from a single direction. 另外,如果仅考虑从一个方向查询它们,是否必须像在示例中一样向Blog和Post添加List导航属性。 ie Blog > Posts, don't need Post Blogs. 即博客>帖子,不需要发布博客。

Would it hurt anything if I add a DbSet BlogPosts to the DbContext even though they don't do it in the example? 即使我在示例中没有将DbSet BlogPosts添加到DbContext,也会对它造成任何伤害吗?

No. The explicit join entity ( BlogPost ) is entity like any other, so you can add (or not add) DbSet for it. 否。显式连接实体( BlogPost )与其他实体一样,因此可以DbSet添加(或不添加) DbSet

If I don't add the DbSet to the DbContext what would be the proper way to create a Blog and then add multiple Posts to it when seeding the database. 如果我不将DbSet添加到DbContext,那么在创建数据库时,创建博客然后在其中添加多个帖子的正确方法是什么。 Something about the below just seems so strange. 以下内容似乎很奇怪。 I would prefer simply creating BlogPost objects and adding them to a BlogPosts in the context. 我希望仅创建BlogPost对象并将它们添加到上下文中的BlogPosts中。

Even if you don't have exposed DbSet<T> property, you can always use Set<T> method to get access to the corresponding DbSet<T> : 即使您没有公开DbSet<T>属性,也可以始终使用Set<T>方法来访问相应的DbSet<T>

context.Set<BlogPost>().Add(new BlogPost { BlogId = blog1.Id, PostId = post1.Id });

or use directly the Add method of the DbContext : 或直接使用DbContextAdd方法:

context.Add(new BlogPost { BlogId = blog1.Id, PostId = post1.Id });

Also, is it mandatory that I add a List navigation property to both the Blog and Post like in the example if I will only care about querying them from a single direction. 另外,如果仅考虑从一个方向查询它们,是否必须像在示例中一样向Blog和Post添加List导航属性。 ie Blog > Posts, don't need Post Blogs. 即博客>帖子,不需要发布博客。

Navigation properties are not mandatory - see Single Navigation Property EF Core documentation topic. 导航属性不是必需的-请参阅“ 单个导航属性 EF核心”文档主题。 You can omit any of them. 您可以忽略其中任何一个。 You don't even need a fluent configuration for the relationships because everything is by convention. 您甚至不需要流利的关系配置,因为一切都是按照惯例进行的。 But in case you use fluent configuration as in the example, make sure to use the WithMany() method without passing lambda/string navigation property (because you don't have it). 但是,如果您如示例中那样使用流畅的配置,请确保在不传递lambda / string导航属性的情况下使用WithMany()方法(因为您没有此属性)。

Would it hurt anything if I add a DbSet BlogPosts to the DbContext even though they don't do it in the example? 即使我在示例中没有将DbSet BlogPosts添加到DbContext,也会对它造成任何伤害吗?

No! 没有! it wouldn't hurt! 不会受伤的! there is no problem of doing so! 这样做没有问题! then you can use as follows: 那么您可以使用以下方法:

context.BlogPosts.Add(new BlogPost { BlogId = blog1.Id, PostId = post1.Id });

If I don't add the DbSet to the DbContext what would be the proper way to create a Blog and then add multiple Posts to it when seeding the database. 如果我不将DbSet添加到DbContext,那么在创建数据库时,创建博客然后在其中添加多个帖子的正确方法是什么。

If you don't want to expose BlogPosts as DbSet<> then you can do as follows: 如果您不想将BlogPosts公开为DbSet<>则可以执行以下操作:

context.Set<BlogPost>().Add(new BlogPost { BlogId = blog1.Id, PostId = post1.Id });

is it mandatory that I add a List navigation property to both the Blog and Post like in the example if I will only care about querying them from a single direction. 如果仅考虑从一个方向查询它们,是否必须像在示例中一样向Blog和Post添加一个List导航属性。 ie Blog > Posts, don't need Post Blogs. 即博客>帖子,不需要发布博客。

No! 没有! its not mandatory. 它不是强制性的。 You can do as follows: 您可以执行以下操作:

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

}

public class Tag
{
    public string TagId { get; set; }
}

public class PostTag
{
    public int PostId { get; set; }
    public Post Post { get; set; }

    public string TagId { get; set; }
    public Tag Tag { get; set; }
}

Then in the DbContext: 然后在DbContext中:

class MyContext : DbContext
{
    public DbSet<Post> Posts { get; set; }
    public DbSet<Tag> Tags { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<PostTag>()
            .HasKey(t => new { t.PostId, t.TagId });

        modelBuilder.Entity<PostTag>()
            .HasOne(pt => pt.Post)
            .WithMany()
            .HasForeignKey(pt => pt.PostId);

        modelBuilder.Entity<PostTag>()
            .HasOne(pt => pt.Tag)
            .WithMany()
            .HasForeignKey(pt => pt.TagId);
    }
}

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

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