0

When we use ASP.NET MVC framework we have the famous AspNetUsers table, which contains an unique Id for each user. I want to generate data in other tables based on the upon created 'fake' users. How can I pull the Id (from AspNetUsers) that was actually generated for other tables like 'ProfileTable'?

for (var i = 0; i < names.Count(); i++)
{
    AspNetUserFaker = new Faker<AspNetUser>().StrictMode(false)
                .RuleFor(a => a.Id, f => Guid.NewGuid().ToString()) // Randomly
                .RuleFor(a => a.FirstName, (f, u) => firstNames[i]) // Preselected known names
                .RuleFor(a => a.LastName, (f, u) => lastNames[i])
                ...
}

then

ProfileFaker = new Faker<ProfileTable>().StrictMode(false)
    // Get Id from the previously created, has to come from AspNetUsers, and only inserted once on this table
    .RuleFor(a => a.MemberID, f => f.IndexFaker) // <-- Need help here
    .RuleFor(a => a.About, f => f.Lorem.Words())
    ...
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
SpcCode
  • 887
  • 2
  • 13
  • 24

1 Answers1

0

This could be solved by storing user IDs in Queue and pops it everytime - to take ID for each user and no more. You should probably also handle the count of generated of ProfileTable by limiting it to the number of existing users.

var ids = new Queue<string>(); //create Queue to store IDs of generated users

for (var i = 0; i < names.Count(); i++)
{
    AspNetUserFaker = new Faker<AspNetUser>()
                .StrictMode(false)
                .RuleFor(a => a.Id, f => Guid.NewGuid().ToString())
                ... 
                .Generate(1); //Assums that the user is generated

    ids.Enqueue(AspNetUserFaker.Id);
}


ProfileFaker = new Faker<ProfileTable>()
    .StrictMode(false)
    .RuleFor(a => a.MemberID, f => ids.Dequeue()) // Sets the value from queue
    ...
    .Generate(ids.Count); //Limit the generation for the amount of users
Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47