- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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);
}
}
}
此验证器的最大缺点是:
验证复杂类型/嵌套对象的属性:
/// <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/
全部: 我们使用 ASP.NET Web 窗体开始了我们的项目。 我们的 ASP.NET C# web 应用程序在以下环境中使用 .NET Framework 4 ASP.NET Web Forms.
我在尝试为 Entity Framework Core 添加迁移到 Code First 项目时遇到错误,这里是详细信息... 我创建了一个新的 ASP.Net Core Web 项目(VS 2017
Error 1 Copying file bin\EntityFramework.SqlServer.xml to obj\Debug\Package\PackageTmp\bin\Entit
我有一个WPF应用程序,我使用了MVVM体系结构,数据库sql server compact 4。 首先是Entityframework.sqlserverCompact数据库,我使用了通用存储库模式
例如,将 products.Where(p => p.LastModifiedOn > someDate) 总是以相同的订单返回产品(无论如何订购,只要每次调用相同的代码时都是一致的)?或者我应该添加
好吧,假设我有一个用户表,然后是一个记录所有登录尝试的表(Id(计数器)、userId、时间以及是否成功) 现在在我的实体模型中,我希望它成为 2 个关联的一个,包含所有成功尝试的时间和一个包含所有不
我正在努力处理如何最好地跨多个 Visual Studio 项目分解域模型。我正在使用 EntityFramework 4 和企业类型模式来合理化一个可以重用于各种应用程序的系统。最终我想得到一组可以
是否可以使用 POCO 和 EF 处理以下情况?我想要一个 Vehicles 表,有一些 bool 字段,如 IsFast、IsVan、IsTruck,然后有一个 Vehicles 类,有一个 Fas
当我通过传递比较泛型类型 TId 的谓词调用 DbSet.FirstOrDefault() 时,出现以下异常: unable to create a constant value of type 's
我正在使用 EF5 将一些数据从一个数据库迁移到另一个数据库。我通常会使用 SQL 来做这样的事情,但是我需要其他功能(比如在 MembershipProvider 中创建用户)并且希望在 EF 中完
我创建了一个单独的项目并将我的模型复制到这个项目中。我引用了 using System.ComponentModel.DataAnnotations 我的模型定义如下: public class F
上下文: 我有两个类在 SQL 中有两个对应的表。 系统信息 EF 版本: SQL 版本:2012 类 public class Employee { public Employee(
我有一个定义了主从关系的 Entity Framework 对象。详细信息对象集合有一个导航属性。 在稍后的代码中,我尝试使用 AutoMapper 将其中一个主对象映射到数据传输对象。但是,数据传输
有没有办法让这个通用 Context.SalesEntity.Where(t=>t.id==3).Delete(); 有点像 private void DoWork(Expression> predi
来自 NHibernate 背景 - 我有一些审计列来反射(reflect)谁最后编辑了一条记录,以及该编辑发生的时间。 我已经覆盖了 SaveChanges 以寻找一个公共(public)基类,并使
我是 EntityFramework 6 的新手,一直在 POCO 中遇到问题。 以下是我的代码 Domain.cs public abstract class BaseEntity { [K
我刚在单元测试项目中遇到一个问题,坚持认为它需要 EntityFramework 的引用,我确信它不需要它。其他项目正在引用单元测试项目正在引用/测试的项目/扩展方法,并且在不引用 EntityFra
下面的代码应该做什么?在此代码中添加包含表的目的是什么,它应该对与 PersonId 相关的 JobType 进行级联删除,还是只删除具有指定 PersonId 的 Person ? db.tblPe
我试图了解延迟加载在 Entity Framework 6 中是如何工作的。在我看来,延迟加载只适用于 DbContext,其中“to-be-loaded”属性已经存在访问过。请查看下面的示例 - 测
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Entity Framework Validation EntityFramework有什么好的灵活的验证框
我是一名优秀的程序员,十分优秀!