gpt4 book ai didi

asp.net-mvc - 在 EF 伙伴类上使用自定义属性 (AttributeTargets.Class) 进行数据验证

转载 作者:行者123 更新时间:2023-12-01 11:06:52 24 4
gpt4 key购买 nike

我有一个具有以下属性的 Entity Framework 生成的类:

public DateTime LaunchDate;
public DateTime ExpirationDate;

我需要强制执行 ExpirationDate > LaunchDate。

我正在使用各种帖子中描述的好友类。我正在将自定义验证属性(在属性上)应用到同一好友类中的其他属性,这些属性正在工作。

因为我需要比较两个属性,所以我直接在类 (AttributeTargets.Class) 上使用属性

这是我的自定义验证属性:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class PropertyMustBeGreaterThanAttribute : ValidationAttribute
{
private const string _defaultErrorMessage = "'{0}' must be greater than '{1}'!";

public PropertyMustBeGreaterThanAttribute(string property, string greaterThan)
: base(_defaultErrorMessage)
{
GreaterThan = greaterThan;
Property = property;
}

public string Property { get; private set; }
public string GreaterThan { get; private set; }

public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
GreaterThan, Property);
}

public override bool IsValid(object value)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
IComparable greaterThan = properties.Find(GreaterThan, true /* ignoreCase */).GetValue(value) as IComparable;
IComparable property = properties.Find(Property, true /* ignoreCase */).GetValue(value) as IComparable;
return greaterThan.CompareTo(property) > 0;
}
}

首先我不确定我需要将属性应用到哪个类:

[MetadataType(typeof(PromotionValidation))]
[PropertyMustBeGreaterThanAttribute("RetailPrice")] // apply it here
public partial class Promotion
{
[PropertyMustBeGreaterThanAttribute("ExpirationDate", "LaunchDate")] // or here ???
public class PromotionValidation
{

其次,它不起作用,我不知道为什么!!!我试过为这两个类添加属性。命中构造函数中的断点 (PropertyMustBeGreaterThanAttribute),但从未调用 IsValid。

已经把我的头发拔出来了....

谢谢!

最佳答案

通过实现 TypeID 让它工作。我将属性分配给部分类:

    [MetadataType(typeof(PromotionValidation))]
[PropertyMustBeGreaterThanAttribute("RetailPrice", "DiscountedPrice")]
[PropertyMustBeGreaterThanAttribute("ExpirationDate", "LaunchDate")]
[PropertyMustBeGreaterThanAttribute("ClaimDeadline", "ExpirationDate")]
public partial class Promotion
{
...

以下是 list ,以备不时之需:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class PropertyMustBeGreaterThanAttribute : ValidationAttribute
{
private const string _defaultErrorMessage = "'{1}' must be greater than '{0}'!";
private readonly object _typeId = new object();

public PropertyMustBeGreaterThanAttribute(string property, string greaterThan)
: base(_defaultErrorMessage)
{
GreaterThan = greaterThan;
Property = property;
}

public string Property { get; private set; }
public string GreaterThan { get; private set; }

public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
GreaterThan, Property);
}

public override bool IsValid(object value)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
IComparable greaterThan = properties.Find(GreaterThan, true /* ignoreCase */).GetValue(value) as IComparable;
IComparable property = properties.Find(Property, true /* ignoreCase */).GetValue(value) as IComparable;
return greaterThan.CompareTo(property) > 0;
}

public override object TypeId
{
get
{
return _typeId;
}
}
}

关于asp.net-mvc - 在 EF 伙伴类上使用自定义属性 (AttributeTargets.Class) 进行数据验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4621210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com