首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在C# EF核心中用预先定义的列表为一列生成假数据,而使用伪生成另一列中的随机字符串?

如何在C# EF核心中用预先定义的列表为一列生成假数据,而使用伪生成另一列中的随机字符串?
EN

Stack Overflow用户
提问于 2021-03-09 07:18:05
回答 1查看 1.3K关注 0票数 0

我有一个包含9项的列表,我希望在标准表中生成确切的9条记录,其中包含StandardName列中的值,并使用伪方法为Description列生成随机值。是否有一种快速简便的方法来处理假人C#?

代码语言:javascript
复制
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"            
};          
代码语言:javascript
复制
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; }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-09 07:57:03

您可以使用此Helper类:

代码语言:javascript
复制
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++];
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66542412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档