C# 将 DataAnnotations 添加到 EntityFramework 中的实体

标签 c# wpf entity-framework data-annotations

我正在使用 ADO.Net Entity Framework 。为了处理输入验证,我尝试使用 DataAnnotations,我在 StavkOverflow 和 Google 上四处寻找,在所有地方我都发现了几乎相同的使用 MetadataType 的例子。但是,我已经尝试了几个小时,但无法让它工作。出于某种原因,EmployeeMetaData 类中的 CustomAttributes 未应用于 上的相应字段/属性员工类。有谁知道为什么会这样?是的,我确信属性类型和名称完全匹配。

感谢任何帮助,我已经坚持了几个小时。 提前致谢。

EntityExtentions.cs

[MetadataType(typeof(EmployeeMetaData))]
public partial class Employee:IDataErrorInfo
{
    public string Error { get { return String.Empty; } }
    public string this[string property]
    {
        get
        {
            return EntityHelper.ValidateProperty(this, property);
        }
    }
}

public class EmployeeMetaData
{
    [Required(AllowEmptyStrings=false, ErrorMessage = "A name must be defined for the employee.")]
    [StringLength(50, ErrorMessage = "The name must  be less than 50 characters long.")]
    public string Name { get; set; }

    [Required(ErrorMessage = "A username must be defined for the employee.")]
    [StringLength(20, MinimumLength = 3, ErrorMessage = "The username must be between 3-20 characters long.")]
    public string Username { get; set; }

    [Required(ErrorMessage = "A password must be defined for the employee.")]
    [StringLength(20, MinimumLength = 3, ErrorMessage = "The password must be between 3-20 characters long.")]
    public string Password { get; set; }
}

EntityHelper.cs

public static class EntityHelper
{
    public static string ValidateProperty(object obj, string propertyName)
    {
        PropertyInfo property = obj.GetType().GetProperty(propertyName);
        object value = property.GetValue(obj, null);
        List<string> errors = (from v in property.GetCustomAttributes(true).OfType<ValidationAttribute>() where !v.IsValid(value) select v.ErrorMessage).ToList();

        // I was trying to locate the source of the error
        // when I print out the number of CustomAttributes on the property it only shows
        // two, both of which were defined by the EF Model generator, and not the ones
        // I defined in the EmployeeMetaData class
        // (obj as Employee).Username = String.Join(", ", property.GetCustomAttributes(true));

        return (errors.Count > 0) ? String.Join("\r\n", errors) : null;
    }
}

最佳答案

我使用了这个(URL 指向有用的文章,我在其中采纳了一些想法):

// http://www.clariusconsulting.net/blogs/kzu/archive/2010/04/15/234739.aspx
/// <summary>
/// Validator provides helper methods to execute Data annotations validations
/// </summary>
public static class DataValidator
{
    /// <summary>
    /// Checks if whole entity is valid
    /// </summary>
    /// <param name="entity">Validated entity.</param>
    /// <returns>Returns true if entity is valid.</returns>
    public static bool IsValid(object entity)
    {
        AssociateMetadataType(entity);

        var context = new ValidationContext(entity, null, null);
        return Validator.TryValidateObject(entity, context, null, true);
    }

    /// <summary>
    /// Validate whole entity
    /// </summary>
    /// <param name="entity">Validated entity.</param>
    /// <exception cref="ValidationException">The entity is not valid.</exception>
    public static void Validate(object entity)
    {
        AssociateMetadataType(entity);

        var context = new ValidationContext(entity, null, null);
        Validator.ValidateObject(entity, context, true);
    }

    /// <summary>
    /// Validate single property of the entity.
    /// </summary>
    /// <typeparam name="TEntity">Type of entity which contains validated property.</typeparam>
    /// <typeparam name="TProperty">Type of validated property.</typeparam>
    /// <param name="entity">Entity which contains validated property.</param>
    /// <param name="selector">Selector for property being validated.</param>
    /// <exception cref="ValidationException">The value of the property is not valid.</exception>
    public static void ValidateProperty<TEntity, TProperty>(TEntity entity, Expression<Func<TEntity, TProperty>> selector) where TEntity : class
    {
        if (selector.Body.NodeType != ExpressionType.MemberAccess)
        {
            throw new InvalidOperationException("Only member access selector is allowed in property validation");
        }

        AssociateMetadataType(entity);

        TProperty value = selector.Compile().Invoke(entity);
        string memberName = ((selector.Body as MemberExpression).Member as PropertyInfo).Name;

        var context = new ValidationContext(entity, null, null);
        context.MemberName = memberName;
        Validator.ValidateProperty(value, context);
    }

    /// <summary>
    /// Validate single property of the entity.
    /// </summary>
    /// <typeparam name="TEntity">Type of entity which contains validated property.</typeparam>
    /// <param name="entity">Entity which contains validated property.</param>
    /// <param name="memberName">Name of the property being validated.</param>
    /// <exception cref="InvalidOperationException">The entity does not contain property with provided name.</exception>
    /// <exception cref="ValidationException">The value of the property is not valid.</exception>
    public static void ValidateProperty<TEntity>(TEntity entity, string memberName) where TEntity : class
    {
        Type entityType = entity.GetType();
        PropertyInfo property = entityType.GetProperty(memberName);

        if (property == null)
        {
            throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, 
                "Entity does not contain property with the name {0}", memberName));
        }

        AssociateMetadataType(entity);

        var value = property.GetValue(entity, null);

        var context = new ValidationContext(entity, null, null);
        context.MemberName = memberName;
        Validator.ValidateProperty(value, context);
    }

    // http://buildstarted.com/2010/09/16/metadatatypeattribute-with-dataannotations-and-unit-testing/
    // Data Annotations defined by MetadataTypeAttribute are not included automatically. These definitions have to be injected.
    private static void AssociateMetadataType(object entity)
    {
        var entityType = entity.GetType();

        foreach(var attribute in entityType.GetCustomAttributes(typeof(MetadataTypeAttribute), true).Cast<MetadataTypeAttribute>())
        {
            TypeDescriptor.AddProviderTransparent(
                new AssociatedMetadataTypeTypeDescriptionProvider(entityType, attribute.MetadataClassType), entityType);
        }
    }
}

此验证器的最大缺点是:

  • 它表现得像蜗牛。如果你在单个实体上执行它并不重要,但如果你想与数百、数千或更多实体一起工作,那就很重要了。
  • 它不支持开箱即用的复杂类型 - 您必须创建特殊属性并在元数据中使用它来验证复杂类型
  • 这是我最后一次使用 DataAnnotations 进行任何业务验证。它们主要用于只有少数实体的 UI 验证。

验证复杂类型/嵌套对象的属性:

/// <summary>
/// Attribute for validation of nested complex type.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class ValidateComplexTypeAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        return DataValidator.IsValid(value);
    }
}

关于C# 将 DataAnnotations 添加到 EntityFramework 中的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6945625/

相关文章:

c# - Telerik RadListBox - 如何默认选择某些项目?

c# - 将文本与数据相结合

WPF XAML 编辑器总是说具有自定义键类型的字典不是集合

c# - 表达式不能包含 lambda 表达式

c# - 使用c#开发这样的winform

c# - 如何监听其他进程的鼠标事件

wpf - 创建使用 Windows 主题的边框

c# - EF Core DDD 多对多

c# - 使用 Table Per Hierarchy 继承在 LINQ to Entities 查询中转换为派生类型

c# - 为什么将一个列表添加到另一个列表,使用添加范围,从第一个列表中删除元素?