I'm trying to seed data in many-to-many relation in my database, but I cannot make a migration. I'm using Entity Framework Core 5.0.6.
Here are my models:
The general idea is: one Tag can belong to many Topics. One Topic can have many Tags.
Tag.cs:
public class Tag
{
public int Id { get; set; }
// [...]
public ICollection<Topic> Topics { get; set; }
public List<TopicTag> TopicTags { get; set; }
}
Topic.cs
public class Topic
{
public int Id { get; set; }
// [...]
public ICollection<Tag> Tags { get; set; }
public List<TopicTag> TopicTags { get; set; }
}
And TopicTag.cs
public class TopicTag
{
public int TopicId { get; set; }
public Topic Topic { get; set; }
public int TagId { get; set; }
public Tag Tag { get; set; }
}
Here is what I have in OnModelCreating
in my ApplicationDbContext
:
// [...]
// Configuring relation
modelBuilder.Entity<Topic>()
.HasMany(topic => topic.Tags)
.WithMany(x => x.Topics)
.UsingEntity<TopicTag>(
j => j
.HasOne(tt => tt.Tag)
.WithMany(t => t.TopicTags)
.HasForeignKey(t => t.TagId),
j => j
.HasOne(tt => tt.Topic)
.WithMany(t => t.TopicTags)
.HasForeignKey(t => t.TopicId),
j => { j.HasKey(t => new {t.TopicId, t.TagId}); }
);
modelBuilder.Entity<Tag>()
.HasMany(tag => tag.Topics)
.WithMany(x => x.Tags)
.UsingEntity<TopicTag>(
j => j
.HasOne(tt => tt.Topic)
.WithMany(t => t.TopicTags)
.HasForeignKey(t => t.TopicId),
j => j
.HasOne(tt => tt.Tag)
.WithMany(t => t.TopicTags)
.HasForeignKey(t => t.TagId),
j => { j.HasKey(t => new {t.TopicId, t.TagId}); }
);
// [...]
// Seeding data
for (int t = -1; t >= -4; t--)
{
builder.Entity<Tag>().HasData(new Tag()
{
Id = t,
Name = "tag" + t
});
}
for (int y = -1; y >= -10; y--)
{
builder.Entity<Topic>().HasData(new Topic()
{
Id = y,
// [...]
});
}
// Finally joining them together
for (int y = -1; y >= -10; y--)
{
builder.Entity<TopicTag>().HasData(new TopicTag()
{
TopicId = y,
TagId = -1 * ((y % 4) + 1)
});
}
As a result, when I try to create new migration, I receive this error:
The value of 'TopicTag.TagId' is unknown when attempting to save changes. This is because the property is also part of a foreign key for which the principal entity in the relationship is not known.
To be honest, I don't understand the meaning of an error message. TopicTag.TagId
is known, because I'm specifying it while creating new TopicTag
in for loop. Moreover, all ids I'm referencing there are created before.