用大量信息播种数据的最佳方法是什么

用大量信息播种数据的最佳方法是什么

本文介绍了使用EntityFramework Core,用大量信息播种数据的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体配置文件,并在 HasData 的帮助下播种数据,如下例所示.

I have an entity configuration file and I seed data with the help of HasData, the example below.

public class PublicationConfiguration : IEntityTypeConfiguration<Publication>
    {
        public void Configure(EntityTypeBuilder<Publication> builder)
        {
            builder.Property(p => p.Image)
                .HasMaxLength(256)
                .HasDefaultValue("default-publication.png")
                .IsRequired();

            builder.Property(p => p.Title)
                .HasMaxLength(256)
                .IsRequired();

            builder.Property(p => p.Value)
                .IsRequired();

            builder.Property(p => p.PublisherId)
                .IsRequired();

            builder.Property(p => p.CategoryId)
                .IsRequired();

            builder.HasOne(p => p.Category)
                .WithMany(categ => categ.Publications)
                .OnDelete(DeleteBehavior.Restrict);

            builder.HasOne(p => p.Publisher)
                .WithMany(pub => pub.Publications)
                .OnDelete(DeleteBehavior.Restrict);

            builder.HasData(
                new Publication {
                    Id = "publication-one",
                    Title = "the first publication",
                    Value = "the content of the very first publication on the web-site",
                    CreatedAt = DateTime.Now,
                    CategoryId = "category-one",
                    PublisherId = "publisher-one",
                    Image = "image"
                },
                new Publication
                {
                    Id = "publication-two",
                    Title = "the second publication",
                    Value = "the content of the second publication on the web-site",
                    CreatedAt = DateTime.Now,
                    CategoryId = "category-one",
                    PublisherId = "publisher-two",
                    Image = "image"
                },
                new Publication
                {
                    Id = "publication-three",
                    Title = "the third publication",
                    Value = "the content of the third publication on the web-site",
                    CreatedAt = DateTime.Now,
                    CategoryId = "category-two",
                    PublisherId = "publisher-one",
                    Image = "image"
                }
            );
        }
    }

如您所见,我有一个名为 Value 的属性,它只是一个字符串,但是我将其更改为字符串数组,并添加了一些真正的信息,表示 Value 将包含一千多个字符,此外,这里只有3个 Publications ,但我想再添加10个.因此,我的播种机看起来巨大而可怕,我不喜欢它.

As you can see I have a property called Value, it's just a string, but I'm going to change it to an array of strings and add some real information meaning Value will contain over a thousand characters, moreover there are only 3 Publications here, but I want to add like 10 more. Thus my seeder will look huge and awful and I do not like it.

所以我想将这些数据移动到其他任何地方,也许移至json文件,然后从该文件读取数据,或者也许有更好的方法,但是我不知道该如何做以及如何做这项权利.

So I'd like to move this data anywhere else, maybe to a json file and then read the data from this file or maybe there is a better way, nevertheless I have no idea how I can do this and how to do this right.

问题是,此问题的最佳解决方案是什么?我很高兴看到解决方案的代码.

The question is, what is the best solution to this problem? And I would be glad to see the solution code.

推荐答案

您可以通过创建json文件来做多个数据种子.

You can do multiple data seeds by creating a json file.

在PublicationConfiguration类中创建一个名为 SeedLargData 的新方法.

Create a new method called SeedLargData in your PublicationConfiguration class.

在此方法中,获取json文件中的数据,将其转换为 List< Publication> ,然后将其返回到Configure方法.

In this method, get the data in the json file, convert it into List<Publication> , and return it to the Configure method.

 public class PublicationConfiguration : IEntityTypeConfiguration<Publication>
    {
        public void Configure(EntityTypeBuilder<Publication> builder)
        {
           builder.Property(p => p.Image)
            .HasMaxLength(256)
            .HasDefaultValue("default-publication.png")
            .IsRequired();

           builder.Property(p => p.Title)
            .HasMaxLength(256)
            .IsRequired();

           builder.Property(p => p.Value)
            .IsRequired();

           builder.Property(p => p.PublisherId)
            .IsRequired();

           builder.Property(p => p.CategoryId)
            .IsRequired();

           builder.HasOne(p => p.Category)
            .WithMany(categ => categ.Publications)
            .OnDelete(DeleteBehavior.Restrict);

           builder.HasOne(p => p.Publisher)
            .WithMany(pub => pub.Publications)
            .OnDelete(DeleteBehavior.Restrict);

            builder.HasData(SeedLargData());
        }

       public List<Publication> SeedLargData()
        {
            var publications= new List<Publication>();
            using (StreamReader r = new StreamReader(@"json file path"))
            {
                string json = r.ReadToEnd();
                publications= JsonConvert.DeserializeObject<List<Publication>>(json);
            }
            return publications;
        }
    }

这篇关于使用EntityFramework Core,用大量信息播种数据的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 16:36