Hello,
I've this configuration in my DBContext class :
modelBuilder.Entity<Topic>()
.HasMany(t => t.Keywords)
.WithMany(k => k.Topics)
.UsingEntity(j => j.ToTable("TopicKeyword"));
I would like to seed data for this association table, but it seems it's not possible. Since it's juste an assiciation table, I don't want to create a useless entity. However, to create a realistic data sample, I need to set up somme associations between my Topics and my Keywords...
And don't know how to do it without creating an entity, I'm not even sure if it's possible.
Here's my seed method:
public static void SeedDummyData(this ModelBuilder builder, ActualDataModel actualData)
{
var dummyData = new DummyDataGeneratorService(actualData).BuildDataSample(100);
builder.Entity<ApplicationUser>().HasData(dummyData.Users);
builder.Entity<IdentityUserRole<string>>().HasData(dummyData.UserRolesMappings);
builder.Entity<ImageCategory>().HasData(dummyData.ImageCategories);
builder.Entity<Image>().HasData(dummyData.Images);
builder.Entity<Keyword>().HasData(dummyData.Keywords);
builder.Entity<Topic>().HasData(dummyData.Topics);
builder.Entity<Summary>().HasData(dummyData.Summaries);
builder.Entity<Chapter>().HasData(dummyData.Chapters);
builder.Entity<Section>().HasData(dummyData.Sections);
builder.Entity<Article>().HasData(dummyData.Articles);
builder.Entity<SectionArticle>().HasData(dummyData.SectionArticles);
builder.Entity<Paragraph>().HasData(dummyData.Paragraphs);
builder.Entity<SummaryAlinea>().HasData(dummyData.SummaryAlineas);
builder.Entity<ParagraphAlinea>().HasData(dummyData.ParagraphAlineas);
//builder.Entity<object>("TopicKeyword").HasData(dummyData.TopicsKeywords);
}
I'm not sure what to do with the last line.
Here's my build data sample method:
public DummyDataModel BuildDataSample(int? count = null)
{
var data = new DummyDataModel();
var superAdminId = _actualData.Users.FirstOrDefault(u => u.UserName == "SuperAdmin").Id;
data.Users = new DummyUserDataSetupService().SetData().ToHashSet();
data.UserRolesMappings = new DummyUserRoleAssociationService().AssignRolesToUserCollection(data.Users, _actualData.Roles).ToHashSet();
data.ImageCategories = new DummyImageCategoriesGeneratorService(superAdminId).GenerateEntities(3).ToHashSet();
data.Images = new DummyImagesGeneratorService(data.ImageCategories, superAdminId).GenerateEntities(100).ToHashSet();
data.Keywords = new DummyKeywordsGeneratorService(superAdminId).GenerateEntities(100).ToHashSet();
data.Topics = new DummyTopicsGeneratorService(_actualData.Themes, data.Keywords, superAdminId).GenerateEntities(count).ToHashSet();
data.TopicsKeywords = new DummyTopicKeywordAssociationService(data.Topics, data.Keywords).AssignKeywordsToTopicCollection().ToHashSet();
//data.Summaries = new DummySumariesGeneratorService(data.Topics, superAdminId).GenerateEntities().ToHashSet();
return data;
}
Here's my set up associations method :
/// <summary>
/// Assigns keywords to multiple topics.
/// </summary>
/// <returns>An enumeration of keyword-topic associations.</returns>
public IEnumerable<object> AssignKeywordsToTopicCollection()
{
foreach (var topic in _topics)
yield return AssignKeywordsToTopic(topic);
}
/// <summary>
/// Assigns keywords to a specific topic.
/// </summary>
/// <param name="topic">The topic to assign keywords to.</param>
/// <returns>An enumeration of keyword-topic associations.</returns>
private IEnumerable<object> AssignKeywordsToTopic(Topic topic)
{
var number = new Random().Next(13) - 5;
if (number <= 0)
yield return null;
for (int i = 0; i < number; i++)
yield return new
{
KeywordsId = _keywords.ElementAt(new Random().Next(_keywords.Count() - 1)).Id,
TopicId = topic.Id,
};
}
Does anyone have an idea?
Thank you in advance.