我要将客户端的数据迁移到新数据库(SQL SERVER),我需要为其验证所有属性。 我尝试添加数据批注,但未按预期工作。
这是我的示例代码:
public class SellerDetailModel
{
[Required, RegularExpression("[0-9]{2}[0-9A-Z]{13}")]
public string GSTNumber;
[Required, StringLength(100, MinimumLength = 3)]
public string LegalName;
[StringLength(100, MinimumLength = 3)]
public string TradingName;
[Required, StringLength(100, MinimumLength = 3)]
public string Address1;
[StringLength(100, MinimumLength = 3)]
public string Address2;
[Required, StringLength(50, MinimumLength = 3)]
public string Location;
[Required, StringLength(6)]
public int PinCode;
[Required, StringLength(2, MinimumLength = 1)]
public string StateCode;
[StringLength(12, MinimumLength = 6)]
public string ContactNumber;
[Required, StringLength(100, MinimumLength = 6)]
public string EmailId;
}
现在,在另一个类(业务逻辑)中,我正在为该类分配值。
//"invoice" is an object having seller related data
SellerDetailModel dataTransferObject = new SellerDetailModel
{
GSTNumber = invoice.SellerGSTNumber,
LegalName = invoice.SellerLegalName,
TradingName = invoice.SellerTradingName,
Address1 = invoice.SellerAddress1,
Address2 = invoice.SellerAddress2,
Location = invoice.SellerLocation,
PinCode = Convert.ToInt32(invoice.SellerPinCode),
StateCode = invoice.SellerStateCode,
ContactNumber = invoice.SellerContactNumber,
EmailId = invoice.SellerEmailId,
};
,但即使添加了必需的属性,正则表达式,字符串长度等...也没有被验证, 它仍然接受所有null,空值。 有人可以帮我如何验证这些属性吗?
如果在迁移过程中发生一些错误,我只想创建一个日志数据。
编辑
我已经尝试了以下方法,但仍然无法正常工作...
public string Validate()
{
ValidationContext context = new ValidationContext(this);
List<ValidationResult> results = new List<ValidationResult>();
bool isValid = Validator.TryValidateObject(this, context, results, true);
if (!isValid)
{
StringBuilder sbrErrors = new StringBuilder();
foreach (var validationResult in results)
{
sbrErrors.AppendLine(validationResult.ErrorMessage);
}
return sbrErrors.ToString();
}
else
return string.Empty;
}
var validation = dataTransferObject.Validate() //always gives string.Empty
答案 0 :(得分:2)
问题在于ValidationContext使用属性而不是字段。
我通过反编译ValidationContext类找到了答案,发现它只会搜索属性。
您需要将代码更改为此,以便将其转换为属性后才能使用。
public class SellerDetailModel
{
[Required, RegularExpression("[0-9]{2}[0-9A-Z]{13}")]
public string GSTNumber { get; set; }
[Required, StringLength(100, MinimumLength = 3)]
public string LegalName { get; set; }
[StringLength(100, MinimumLength = 3)]
public string TradingName { get; set; }
[Required, StringLength(100, MinimumLength = 3)]
public string Address1 { get; set; }
[StringLength(100, MinimumLength = 3)]
public string Address2 { get; set; }
[Required, StringLength(50, MinimumLength = 3)]
public string Location { get; set; }
[Required, StringLength(6)]
public string PinCode { get; set; }
[Required, StringLength(2, MinimumLength = 1)]
public string StateCode { get; set; }
[StringLength(12, MinimumLength = 6)]
public string ContactNumber { get; set; }
[Required, StringLength(100, MinimumLength = 6)]
public string EmailId { get; set; }
public string Validate()
{
ValidationContext context = new ValidationContext(this);
List<ValidationResult> results = new List<ValidationResult>();
bool isValid = Validator.TryValidateObject(this, context, results, true);
if (!isValid)
{
StringBuilder sbrErrors = new StringBuilder();
foreach (ValidationResult validationResult in results)
{
sbrErrors.AppendLine(validationResult.ErrorMessage);
}
return sbrErrors.ToString();
}
else
return string.Empty;
}
}
看起来您也不需要在Invoice上转换为int。PinCode字段或Validation函数会因为查找字符串而被炸毁。
public class Invoice
{
public string SellerGSTNumber { get; set; }
public string SellerLegalName { get; set; }
public string SellerTradingName { get; set; }
public string SellerAddress1 { get; set; }
public string SellerAddress2 { get; set; }
public string SellerLocation { get; set; }
public string SellerPinCode { get; set; }
public string SellerStateCode { get; set; }
public string SellerContactNumber { get; set; }
public string SellerEmailId { get; set; }
}
public class Mapper
{
public static SellerDetailModel Map(Invoice invoice)
{
SellerDetailModel dataTransferObject = new SellerDetailModel
{
GSTNumber = invoice.SellerGSTNumber,
LegalName = invoice.SellerLegalName,
TradingName = invoice.SellerTradingName,
Address1 = invoice.SellerAddress1,
Address2 = invoice.SellerAddress2,
Location = invoice.SellerLocation,
PinCode = invoice.SellerPinCode,
StateCode = invoice.SellerStateCode,
ContactNumber = invoice.SellerContactNumber,
EmailId = invoice.SellerEmailId,
};
return dataTransferObject;
}
}