简体   繁体   English

为什么我在 FluentValidation MustAsync 中收到 CS1061 错误

[英]Why am I receiving CS1061 error in FluentValidation MustAsync

I'm trying to create some validation rules using static extension methods for IRuleBuilderOptions so I don't have to keep repeating myself when creating validators for Creating and Updating my objects.我正在尝试使用IRuleBuilderOptions静态扩展方法创建一些验证规则,因此在创建用于创建和更新我的对象的验证器时,我不必不断重复自己。

For some reason I keep getting CS1061 errors for the country.Id in the MustAsync unique checks.出于某种原因,我不断收到CS1061错误的country.IdMustAsync独特的检查。 I have tried replacing arrow functions with brackets and return , tried async / await .我试过用括号替换箭头函数并return ,试过async / await They all result in the same error.它们都会导致相同的错误。 Intellisense shows that country is of type Country , and Id is a public property with both public getter and setter, so I'm not sure what I'm missing? Intellisense 显示countryCountry类型,而Id是具有公共 getter 和 setter 的公共属性,所以我不确定我错过了什么? I tried compiling in case it was just an Intellisense issue, but compilation fails with the same error.我尝试编译,以防它只是 Intellisense 问题,但编译失败并出现相同的错误。

Note: Using VS2022 / .Net6注意:使用 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";
}

Here is a screenshot showing proper typeing of the country parameter and the error I am getting...这是一个屏幕截图,显示了正确键入country参数和我收到的错误...

正确的参数类型,但访问属性时出错

Because Country is the name of generic parameter of your methods, not a type name.因为Country是方法的泛型参数的名称,而不是类型名称。 Just remove <Country> from the signatures:只需从签名中删除<Country>

public static IRuleBuilderOptions<Country, string> Name(...
public static IRuleBuilderOptions<Country, string> Code2(...
public static IRuleBuilderOptions<Country, string> Code3(...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM