gpt4 book ai didi

c# - 伪造模拟数据 2 个不同属性的相同值

转载 作者:行者123 更新时间:2023-11-30 21:26:37 27 4
gpt4 key购买 nike

伪造的模拟数据相同值对于 2 个不同的属性,是否有可能在 Fluent API 中让 2 个属性具有相同的值。

var users = new Faker<User>()
.StrictMode(false)
.RuleFor(o => o.Id, f => orderIds++)
.RuleFor(o => o.UserName, f => f.Person.FullName) // This needs to be same as the next property
.RuleFor(o => o.NormalizedUserName, f => f.Person.FullName) // This should be same but uppercase

想要生成的数据:

[
{
userName: "Ivan Horvat",
normalizedUserName: "IVAN HORVAT"
},
{
userName: "John Doe",
normalizedUserName: "JOHN DOE"
}
]

我希望生成的每个实体都具有相同的 UserNameNormalizedUsername 但每个实体都有自己的。

最佳答案

您也可以使用 RuleFor(Prop, (f, usr) =>) 重载来拥有两个具有相同值的属性。

void Main()
{
int orderIds = 0;
var users = new Faker<User>()
.StrictMode(false)
.RuleFor(o => o.Id, f => orderIds++)
.RuleFor(o => o.UserName, f => f.Person.FullName) // This needs to be same as the next property
.RuleFor(o => o.NormalizedUserName, (f, usr) => usr.UserName.ToUpper()); // This should be same but uppercase

users.Generate(3).Dump();
}

public class User{
public int Id{get;set;}
public string UserName{get;set;}
public string NormalizedUserName {get;set;}
}

results

关于c# - 伪造模拟数据 2 个不同属性的相同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58996414/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com