[英]Validation failed for one or more entities in Entity Framework for nullable boolean property
[英]FluentValidation - how to have one entity property value drive another entities validation
我有一個包含多個屬性和多個集合的對象,一個簡化的示例:
"Form": {
"Funders": [
{
"FunderID": "string",
"Name": "string",
"Type": "string"
}
],
"Publications": [
{
"PublicationID": "string",
"Name": "string",
"Type": "string",
}
],
"Created": "2017-02-10T22:26:49.528Z",
"CreatedBy": "string",
"LastModified": "2017-02-10T22:26:49.528Z",
"LastModifiedBy": "string",
}
如果資金提供Funders
中的任何Funders
使用流利的驗證而具有A Type
,那么我如何驗證Publications
至少具有1種Type
Z的Publication
。
如果任何資助者類型為A類型,則以另一種方式陳述,則出版物中的一個出版物必須為Z類型。
目前,我有一些針對Publisher,Funder和Form的驗證器,只是想弄清楚如何創建此驗證。
我正在使用fluentvalidation,C#,實體框架。
答案是從整個模型的驗證中執行驗證。 實際上很簡單。
RuleFor(x => x.Publications
.Where(p => p.Type == "Type Z").Count()).GreaterThan(0)
.When(x => x.Funders.Where(f => f.Type == "Type A").Count() > 0)
.WithMessage("An Form containing a funder of type 'Type A' must have at least one Publication with of type 'Type Z'.")
.OverridePropertyName("Publications");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.