为啥我在 FluentValidation MustAsync 中收到 CS1061 错误
Posted
技术标签:
【中文标题】为啥我在 FluentValidation MustAsync 中收到 CS1061 错误【英文标题】:Why am I receiving CS1061 error in FluentValidation MustAsync为什么我在 FluentValidation MustAsync 中收到 CS1061 错误 【发布时间】:2021-11-09 03:33:25 【问题描述】:我正在尝试使用IRuleBuilderOptions
的静态扩展方法创建一些验证规则,因此在为创建和更新我的对象创建验证器时我不必不断重复自己。
由于某种原因,我在MustAsync
唯一检查中不断收到country.Id
的CS1061 错误。我试过用括号和return
替换箭头函数,试过async
/await
。它们都导致相同的错误。 Intellisense 显示country
的类型为Country
,而Id
是具有公共getter 和setter 的公共属性,所以我不确定我缺少什么?我尝试编译以防它只是一个 Intellisense 问题,但编译失败并出现同样的错误。
注意:使用 VS2022 / .Net6
public class Country
public int Id get; set;
public string Name get; set; = string.Empty;
public string Code2 get; set; = string.Empty;
public string Code3 get; set; = string.Empty;
public int DisplayOrder get; set; = 1000;
public bool Enabled get; set; = true;
public class CountryCreateValidator : AbstractValidator<Country>
public CountryCreateValidator(IApplicationDbContext dbContext)
RuleFor(x => x.Name).Name(dbContext);
RuleFor(x => x.Code2).Code2(dbContext);
RuleFor(x => x.Code3).Code3(dbContext);
public class CountryUpdateValidator : AbstractValidator<Country>
public CountryUpdateValidator(IApplicationDbContext dbContext)
RuleFor(x => x.Id).NotEmpty();
RuleFor(x => x.Name).Name(dbContext);
RuleFor(x => x.Code2).Code2(dbContext);
RuleFor(x => x.Code3).Code3(dbContext);
public static class CountryRules
public static IRuleBuilderOptions<Country, string> Name<Country>(this IRuleBuilder<Country, string> ruleBuilder, IApplicationDbContext dbContext) =>
ruleBuilder.NotNull().NotEmpty().MaximumLength(64)
.MustAsync((country, name, cancellationToken) =>
return dbContext.Countries.AllAsync(x => x.Id == country.Id /* error here */ || x.Name != name, cancellationToken);
).WithMessage(FvConstants.UNIQUE);
public static IRuleBuilderOptions<Country, string> Code2<Country>(this IRuleBuilder<Country, string> ruleBuilder, IApplicationDbContext dbContext) =>
ruleBuilder.NotEmpty().Length(2).Matches("^[A-Z]2$").WithMessage("PropertyName must be two uppercase letters")
.MustAsync((country, code2, cancellationToken) =>
return dbContext.Countries.AllAsync(x => x.Id == country.Id || x.Code2 != code2, cancellationToken);
).WithMessage(FvConstants.UNIQUE);
public static IRuleBuilderOptions<Country, string> Code3<Country>(this IRuleBuilder<Country, string> ruleBuilder, IApplicationDbContext dbContext) =>
ruleBuilder.NotEmpty().Length(3).Matches("^[A-Z]3$").WithMessage("PropertyName must be three uppercase letters")
.MustAsync((country, code3, cancellationToken) =>
return dbContext.Countries.AllAsync(x => x.Id == country.Id || x.Code3 != code3, cancellationToken);
).WithMessage(FvConstants.UNIQUE);
public class FvConstants
public const string UNIQUE = "PropertyName must be unique";
这是一个屏幕截图,显示了正确键入 country
参数以及我遇到的错误...
【问题讨论】:
【参考方案1】:因为Country
是您的方法的泛型参数的名称,而不是类型名称。只需从签名中删除<Country>
:
public static IRuleBuilderOptions<Country, string> Name(...
public static IRuleBuilderOptions<Country, string> Code2(...
public static IRuleBuilderOptions<Country, string> Code3(...
【讨论】:
谢谢。我不敢相信我错过了。正在查看 FluentValidation 文档,他们的示例IRuleBuilderOptions
扩展方法适用于泛型类型。我用Country
替换了所有T
s,并在删除通用参数时完全隔开。以上是关于为啥我在 FluentValidation MustAsync 中收到 CS1061 错误的主要内容,如果未能解决你的问题,请参考以下文章