1

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>

1 Answer 1

1

You can simply create navigation property as

public class Person
{
    public int PersonID { get; set; } 
    public bool ShowPerson { get; set; } 
    public int Age { get; set; }
    public string Name { get; set;}
    public virtual List<UserText> ListOfTexts{ get; set; }
}

public class UserText
{
    public int UserTextID { get; set; }
    [Required]
    public string Text { get; set; }        
}

with this I hope there is no issue as .net core creates relationships based on navigation property ListOfTexts created in Person Model.

Sign up to request clarification or add additional context in comments.

1 Comment

hm, so strange, adding this still has not made the error stop, it still asks for navigation property, i apologize as im rather new at this, but thank you so kindly for your reply, i will also keep looking, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.