I am trying to add a list inside the object person
public class Person
{
[Key]
public int Id { get; set; }
public bool ShowPerson { get; set; } //To show this person on the main database for the client side
public int Age { get; set; }
public string Name { get; set;}
public List<UserText> ListOfTexts{ get; set; } //this list
}
public class UserText
{
public int Id { get; set; }
[Required]
public string Text { get; set; }
}
I'm trying to make my object be able to read the list inside of my object (Person)
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public DbSet<Person> People { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Person>().HasData(new Person
{
Id = 1,
ShowPerson = true,
Age = 12,
Name = "John",
ListOfTexts = new list<UserText>()
{
new UserText{ Id = 1, Text = "I want to keep things simple"},
new UserText{ Id = 2, Text = "I want to keep things clean"}
},
});
}
}
When I try to migrate the database, it get this error:
The seed entity for entity type 'Person' cannot be added because it has the navigation 'ListOfTexts' set. To seed relationships, add the entity seed to 'UserText' and specify the foreign key values {'PersonId'}. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the involved property values.
What can I do so I can properly do it?
I want the user to manually add/remove as many Texts inside their data (person)
Show all the texts in their details page inside blazor
Example:
<div>
@foreach (var textline in Person.ListOfTexts)
{
<p>@textline.Text</p>
}
@*Rest Of Code*@
</div>