使用 EntityFramework Core 植入包含大量信息的数据的最佳方法是什么?

Dmi*_*iuk 4 c# database entity-framework-core asp.net-core asp.net-core-webapi

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

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"
                }
            );
        }
    }
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我有一个名为 的属性Value,它只是一个字符串,但我要将其更改为字符串数组并添加一些真实信息,含义Value将包含一千多个字符,而且这里只有 3 个字符Publications,但我想再添加10个左右。因此我的播种机看起来又大又丑,我不喜欢它。

所以我想将这些数据移动到其他地方,也许移动到 json 文件,然后从该文件读取数据,或者也许有更好的方法,但我不知道如何做到这一点以及如何正确执行此操作。

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

Dmi*_*iuk 10

上面的答案有效,但我让它可重用。

这是结果。

public static class SeedHelper
    {
        public static List<TEntity> SeedData<TEntity>(string fileName)
        {
            string currentDirectory = Directory.GetCurrentDirectory();
            string path = "Static/Json";
            string fullPath = Path.Combine(currentDirectory, path, fileName);

            var result = new List<TEntity>();
            using (StreamReader reader = new StreamReader(fullPath))
            {
                string json = reader.ReadToEnd();
                result = JsonConvert.DeserializeObject<List<TEntity>>(json);
            }

            return result;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我希望你明白这"Static/Json"是我的 json 文件所在的路径。


Lou*_*raQ 7

您可以通过创建 json 文件来执行多个数据种子。

SeedLargData创建一个在 PublicationConfiguration 类中调用的新方法。

在该方法中,获取json文件中的数据,将其转换为List<Publication>,返回给Configure方法。

 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;
        }
    }    
Run Code Online (Sandbox Code Playgroud)