【发布时间】:2015-11-03 00:58:50
【问题描述】:
总结:
我希望数据注释验证器引用同一类中的另一个属性 (TitleAuthorAndPublishingConfiguration)。
但是,DB.SaveChanges() 并未直接在此类上调用。而是在此类的父级 (WebsiteConfiguration) 上调用它。
因此validationContext.ObjectType 正在返回WebsiteConfiguration,我无法在数据注释验证器中引用TitleAuthorAndPublishingConfiguration 的属性。
WebsiteConfiguration.cs
public class WebsiteConfiguration
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public TitleAuthorAndPublishingConfiguration TitleAuthorAndPublishing { get; set; }
public BookChaptersAndSectionsConfiguration BookChaptersAndSections { get; set; }
public SocialMediaLoginsConfiguration SocialMediaLogins { get; set; }
public TagGroupsConfiguration TagGroups { get; set; }
}
public class TitleAuthorAndPublishingConfiguration
{
public string BookTitle { get; set; }
public bool IsPublished { get; set; }
// how do I access a property of current model when calling DB.SaveChanges() on parent?
[RequiredIfOtherFieldIsEnabled("IsPublished")]
public string Publisher { get; set; }
}
// ... and other sub models...
ApplicationDbContext.cs
DbSet<WebsiteConfiguration> WebsiteConfiguration {get;set;}
更新代码示例
public void SeedWebsiteConfiguration()
{
var titleAuthorAndPublishingConfiguration = new TitleAuthorAndPublishingConfiguration()
{
// seed values
};
var bookChaptersAndSectionsConfiguration = new BookChaptersAndSectionsConfiguration()
{
// seed values
};
var socialMediaLoginConfiguration = new SocialMediaLoginsConfiguration()
{
// seed values
};
var tagGroupsConfiguration = new TagGroupsConfiguration()
{
// seed values
};
var websiteConfiguration = new WebsiteConfiguration()
{
TitleAuthorAndPublishing = titleAuthorAndPublishingConfiguration,
BookChaptersAndSections = bookChaptersAndSectionsConfiguration,
SocialMediaLogins = socialMediaLoginConfiguration,
TagGroups = tagGroupsConfiguration
};
DB.WebsiteConfiguration.Add(websiteConfiguration);
DB.SaveChanges();
}
验证码
public class RequiredIfOtherFieldIsEnabledAttribute : ValidationAttribute
{
private string _ifWhatIsEnabled { get; set; }
public RequiredIfOtherFieldIsEnabledAttribute(string IfWhatIsEnabled)
{
_ifWhatIsEnabled = IfWhatIsEnabled;
}
protected override ValidationResult IsValid(object currentPropertyValue, ValidationContext validationContext)
{
var isEnabledProperty = validationContext.ObjectType.GetProperty(_ifWhatIsEnabled);
if (isEnabledProperty == null)
{
return new ValidationResult(
string.Format("Unknown property: {0}", _ifWhatIsEnabled)
);
}
var isEnabledPropertyValue = (bool)isEnabledProperty.GetValue(validationContext.ObjectInstance, null);
if (isEnabledPropertyValue == true)
{
if (String.IsNullOrEmpty(currentPropertyValue.ToString()))
{
return new ValidationResult(String.Format("This field is required if {0} is enabled", isEnabledProperty));
}
}
return ValidationResult.Success;
}
}
问题
我可以通过
validationContext访问子模型属性吗?我的方法是否被误导了?有没有更好的方法将多个模型作为更大模型的一部分存储在单个数据库表中?
我希望不要有多个配置表和对数据库的调用。 (本例中有 4 个子模型,但下一个应用中可能有 10+ 个。)
上面的设置在很多方面都满足了我的需求。但是我不想放弃子模型上DataAnnotations的功能!
奖金问题
我遇到过一些类似这样的帖子: How can I tell the Data Annotations validator to also validate complex child properties?
但那已经是 4 岁了,我想知道从那时起是否有任何变化。
我是否正在尝试做一些基本上不可能(或至少非常困难)的事情?
【问题讨论】:
-
我想要
TitleAuthorAndPublishingConfiguration的属性上的数据注释验证器来引用TitleAuthorAndPublishingConfiguration的另一个属性。但是DB.SaveChanges()并没有直接在这个类上被调用,而是在它的父级上 -WebsiteConfiguration。 -
我已经更新了问题,希望能更清楚
-
你真的有
TitleAuthorAndPublishingConfiguration验证器吗? -
我在问题中添加了验证器代码 - 这就是您的意思吗?
标签: asp.net-mvc data-annotations dbcontext