bchavez / Bogus

:card_index: A simple fake data generator for C#, F#, and VB.NET. Based on and ported from the famed faker.js.
Other
8.66k stars 495 forks source link

Parent have children && one to many #380

Closed HelloValue closed 3 years ago

HelloValue commented 3 years ago

Version Information

Software Version(s)
Bogus NuGet Package 33.0.2
.NET Core? /
.NET Full Framework? .NET 5
Windows OS? /
Linux OS? /
Visual Studio? 2019

What locale are you using with Bogus?

.NET 5

What's the problem?

I have two class:

public class  Order{
    public string OrderId { get; set; }
    public string EncounterType {get;set;}
    public string OrderCreateTime {get;set;}
    public List<TestItem> TestItems { get; set; }
  }

  public class  TestItem {
    public string OrderId { get; set; }
    public String ItemCode { get; set; }
  }

i want generate 10 order, and OrderId is different. then it contains any TestItem and OrderId should be Order's OrderId.

Thanks.

Do you have sample code to show what you're trying to do?

var itemcodeGenerator = new Faker<TestItem>()
             .RuleFor(o => o.OrderId, Guid.NewGuid().ToString())
             .RuleFor(o => o.ItemCode, f => f.Name.ItemCode());

            var itemcodes = itemcodeGenerator.Generate(10).ToList();

            var orderGenerator = new Faker<Order>()
             .RuleFor(c => c.Diagnosis, f => f.Phone.PhoneNumber())
             .RuleFor(c => c.OrderId, f => Guid.NewGuid().ToString())
             .RuleFor(c => c.EncounterType, f => f.Name.EncounterType())
             .RuleFor(c => c.TestItems, f => itemcodeGenerator.Generate(f.Random.Number(8)).ToList());

var orders  =  orderGenerator.Generate(10);

(Please be complete. Provide all code necessary to run your example in LINQPad.) (The more complete code examples are, the more accurate answers will be.) (https://www.linqpad.net)

HelloValue commented 3 years ago

++,This is the json data I want:

[{
        "OrderId":"1",
        "EncounterType":"S",
        "OrderCreateTime":"20210401",
        "TestItem":[
          {"OrderId":"1","ItemCode":"K"},{"OrderId":2,"ItemCode":"NA"}
        ]

      },
      {
        "OrderId":"2",
        "EncounterType":"R",
        "OrderCreateTime":"20210401",
        "TestItem":[
          {"OrderId":"2","ItemCode":"K1"},{"OrderId":2,"ItemCode":"NA2"}
        ]
}]
davidbuckleyni commented 3 years ago

I two am wanting this u think something so simple would be answered quick but been still waiting on an answer @HelloValue

bchavez commented 3 years ago

Please read the examples here for Blog (parent) and Post (child) relationships: https://github.com/bchavez/Bogus/blob/9fc7614274f7f80b889151aa31a5fd2f2d0e610d/Examples/EFCoreSeedDb/Program.cs#L64-L153

HelloValue commented 3 years ago

@davidbuckleyni @bchavez Thanks.

Sorry, it's my problem, I didn't find the post.

bchavez commented 3 years ago

@HelloValue , no worries. Let me know if you have any other questions.

Feel free to post in https://github.com/bchavez/Bogus/discussions