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"
}
);
}
}
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.
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.