1

According to the official example it is possible to generate seed data for a child while generating the parent.

    int commentId = 1;
    var comments = new Faker<CommentModel>()
        .RuleFor(m => m.Id, f => commentId++)
        .RuleFor(m => m.Content, f => f.Commerce.ProductDescription());

    int discussionId = 1;
    var discussionData = new Faker<DiscussionModel>()
        .RuleFor(m => m.Id, f => discussionId++)
        .RuleFor(m => m.Comments, f => comments.Generate(f.Random.Int(0,25)).ToList())
        .Generate(35);

    builder.Entity<DiscussionModel>().HasData(discussionData);

However, when I do this, I get an error

The seed entity for entity type 'DiscussionModel' cannot be added because it has the navigation 'Comments' set. To seed relationships, add the entity seed to 'CommentModel' and specify the foreign key values {'DiscussionId'}.

Which is how I would have thought to build the data, except that the example way has much better utility. Am I doing something wrong, or is the example inaccurate?

(this question is close but not a duplicate, the author is trying to add records by key, which may be what Bogus is trying to do behind the scenes. Either way, the answers there don't seem to work when applied to my situation)

Randy Hall
  • 7,716
  • 16
  • 73
  • 151

0 Answers0