1

我正在尝试下一条规则:

var result = new AutoFaker<MyModel>().RuleFor(x => x.AnotherModel, null).Generate();

public class MyModel
{
    public string Test { get; set; }
    public AnotherModel AnotherModel { get; set; }
}

public class AnotherModel
{
    public string Test1 { get; set; }
}

得到消息:

Severity    Code    Description Project File    Line    Suppression State
Error   CS0121  The call is ambiguous between the following methods or properties: 
'Faker<T>.RuleFor<TProperty>(Expression<Func<T, TProperty>>, Func<Faker, T, TProperty>)' 
and 'Faker<T>.RuleFor<TProperty>(Expression<Func<T, TProperty>>, TProperty)'    

为什么我不能将 null 分配给该模型?

4

1 回答 1

1

以下应该有效:

void Main()
{
   var result = new AutoFaker<MyModel>()
         .RuleFor(x => x.AnotherModel, _ => null);
         
   result.Generate().Dump();
}

public class MyModel
{
   public string Test { get; set; }
   public AnotherModel AnotherModel { get; set; }
}

public class AnotherModel
{
   public string Test1 { get; set; }
}

结果

有一个模棱两可的电话的原因是因为您需要更具体地了解您想要使用的“规则”方法。例如:.RuleFor(expr, value) 或者.RuleFor(expr, Func<T>)等等……

谢谢,希望对您有所帮助,
布赖恩查韦斯

于 2022-01-29T16:50:52.970 回答