I have an entity JournalEntry which has a many-to-many relationship with Tags thorugh a jointable JournalEntryTags.
I want to create a journalentry uppon submittion of a form containing the journal content and a comma separated list of tags. If the tags does not exist in the db, they should be created. The relationship between the journalentry and new and old tags should be created.
This is the AddEntry method I've got in my JournalEntryRepository:
public void AddEntry(JournalEntry journalEntry, string tags)
{
journalEntry.CreatedAt = DateTime.Now;
_appDbContext.JournalEntries.Add(journalEntry);
var tagsArray = tags.Split(',');
foreach (string tag in tagsArray)
{
if (_tagRepository.GetTagByName(tag.Trim()) is null)
{
_tagRepository.AddTag(new Tag { Name = tag.Trim() });
}
}
_appDbContext.SaveChanges();
}
How do I create the relationship JournalEntryTags from here? What do I need to know in order to make that happen?