我想使用 Bogus
库创建假数据来测试数据库性能。这是我的书示例:
public class Book
{
public Guid Id { get; set; }
public string Title { get; set; }
public double Price { get; set; }
public string ISBN { get; set; }
public Genre Genre { get; set; }
}
public enum Genre
{
Poetry,
Romance,
Thriller,
Travel
}
}
此外,我还为 Book
类中的每个属性制定了生成虚假数据的规则。
public static Faker<Book> Book = new Faker<Book>()
.StrictMode(true)
.RuleFor(x => x.Id, f => f.Random.Guid())
.RuleFor(x => x.Title, f => f.Company.CompanyName())
.RuleFor(x => x.Price, f => f.Random.Double())
.RuleFor(x => x.ISBN, f => $"ISBN_{f.Random.Guid()}")
.RuleFor(x => x.Genre, f => ???);
我的问题是 - 如何为枚举 Genre
生成随机值
提前致谢!
最佳答案
答案可以在 the documentation 中找到本身,
尝试使用
.RuleFor(x => x.Genre, f =>f.PickRandom<Genre>());
你的代码看起来像,
public static Faker<Book> Book = new Faker<Book>()
.StrictMode(true)
.RuleFor(x => x.Id, f => f.Random.Guid())
.RuleFor(x => x.Title, f => f.Company.CompanyName())
.RuleFor(x => x.Price, f => f.Random.Double())
.RuleFor(x => x.ISBN, f => $"ISBN_{f.Random.Guid()}")
.RuleFor(x => x.Genre, f => f.PickRandom<Genre>());
关于c# - 在Bogus库中为Enum生成假数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68168067/