如何在 C# EF Core 中使用 Bogus 在另一列中生成随机字符串时为一列生成带有预定义列表的假数据?

How to generate fake data with pre-defined list for one column while use Bogus to generate random string in another column in C# EF Core?

我有一个包含 9 个项目的列表,我想使用 列表中的值在 标准 table 中生成确切的 9 条记录StandardName 列并使用 Bogus 为 Description 列生成随机值。使用 Bogus C# 是否有快速简便的方法?

var standardNames = new List<string>()
{
    "English Language Arts Standards",
    "Mathematics Standards",    
    "Fine Arts Standards",
    "Language Arts Standards",
    "Mathematics Standards",
    "Physical Education and Health Standards",
    "Science Standards",
    "Social Sciences Standards",
    "Technology Standards"            
};          

using System.Collections.Generic;

namespace EFCore_CodeFirst.Model.School
{
    public class Standard
    {
        public Standard()
        {
            this.Students = new HashSet<Student>();
            this.Teachers = new HashSet<Teacher>();
        }

        public int StandardId { get; set; }
        public string StandardName { get; set; }
        public string Description { get; set; }

        public virtual ICollection<Student> Students { get; set; }
        public virtual ICollection<Teacher> Teachers { get; set; }
    }
}

您可以使用这个助手class:

class Helper
{
    static List<string> standardNames = new List<string>()
    {
        "English Language Arts Standards",
        "Mathematics Standards",
        "Fine Arts Standards",
        "Language Arts Standards",
        "Mathematics Standards",
        "Physical Education and Health Standards",
        "Science Standards",
        "Social Sciences Standards",
        "Technology Standards"
    };

    static int nameIndex = 0;

    public static List<Standard> GetSampleTableData()
    {
        var index = 1;

        var faker = new Faker<Standard>()
            .RuleFor(o => o.StandardId, f => index++)
            .RuleFor(o => o.StandardName, a => GetNextName())
            .RuleFor(o => o.Description, f => f.Random.String(25));

        return faker.Generate(standardNames.Count);
    }

    private static string GetNextName()
    {
        if (nameIndex >= standardNames.Count)
            nameIndex = 0;
        return standardNames[nameIndex++];
    }
}