为什么我在 FluentValidation MustAsync 中收到 CS1061 错误
Why am I receiving CS1061 error in FluentValidation MustAsync
我正在尝试使用静态扩展方法为 IRuleBuilderOptions
创建一些验证规则,这样在创建用于创建和更新我的对象的验证器时就不必不断重复自己了。
出于某种原因,我在 MustAsync
唯一检查中不断收到 country.Id
的 CS1061 错误。我试过用括号和 return
替换箭头函数,尝试过 async
/await
。它们都会导致相同的错误。 Intellisense 显示 country
是 Country
类型,而 Id
是一个 public 属性,同时具有 public 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
参数和我收到的错误的屏幕截图...
因为Country
是你方法的泛型参数名,不是类型名。只需从签名中删除 <Country>
:
public static IRuleBuilderOptions<Country, string> Name(...
public static IRuleBuilderOptions<Country, string> Code2(...
public static IRuleBuilderOptions<Country, string> Code3(...
我正在尝试使用静态扩展方法为 IRuleBuilderOptions
创建一些验证规则,这样在创建用于创建和更新我的对象的验证器时就不必不断重复自己了。
出于某种原因,我在 MustAsync
唯一检查中不断收到 country.Id
的 CS1061 错误。我试过用括号和 return
替换箭头函数,尝试过 async
/await
。它们都会导致相同的错误。 Intellisense 显示 country
是 Country
类型,而 Id
是一个 public 属性,同时具有 public 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
参数和我收到的错误的屏幕截图...
因为Country
是你方法的泛型参数名,不是类型名。只需从签名中删除 <Country>
:
public static IRuleBuilderOptions<Country, string> Name(...
public static IRuleBuilderOptions<Country, string> Code2(...
public static IRuleBuilderOptions<Country, string> Code3(...