C# 按名称获取类属性
我正在重写ValidateEntity方法以检查唯一验证,但遇到了一个绊脚石C# 按名称获取类属性,c#,entity-framework,casting,C#,Entity Framework,Casting,我正在重写ValidateEntity方法以检查唯一验证,但遇到了一个绊脚石 protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items) { var result = new DbEntityValidationResult(entityEntry, new List<DbValidatio
protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items)
{
var result = new DbEntityValidationResult(entityEntry, new List<DbValidationError>());
if (entityEntry.Entity is ReferenceType && entityEntry.State == EntityState.Added)
{
var entity = entityEntry.Entity as ReferenceType;
var pluralService = PluralizationService.CreateService(CultureInfo.GetCultureInfo("en-gb"));
var pluralEntity = pluralService.Pluralize(entity.GetType().Name);
// I would like Courses to be replaced with the property name of pluralEntity
if (Courses.Where(x => x.Title == entity.Title).Count() > 0)
{
result.ValidationErrors.Add(new DbValidationError(nameof(entity.Title), nameof(entity.Title) + " must be unique."));
}
}
if (result.ValidationErrors.Count > 0)
{
return result;
}
else
{
return base.ValidateEntity(entityEntry, items);
}
}
有办法做到这一点吗
更新
我有这个:
var prop = (DbSet<ReferenceType>) GetType().GetProperty(pluralEntity).GetValue(this, null);
if (prop.Where(x => x.Title == entity.Title).Count() > 0)
{
result.ValidationErrors.Add(new DbValidationError(nameof(entity.Title), nameof(entity.Title) + " must be unique."));
}
当然,这是一个变量,不能作为泛型类型传入。目前我唯一能想到的是使用存储库模式编写一个自定义验证方法 首先,创建一个所有实体都将实现的接口
public interface IEntity
{
public string Title {get; set; }
}
然后创建存储库:
public class Repository<TEntity> where TEntity: class, IEntity
{
private YourContext context = new YourContext();
private DbSet<TEntity> AppDbSet;
public Repository()
{
AppDbSet = context.Set<TEntity>();
}
//a couple of method to retrieve data...
public List<TEntity> GetAll()
{
return AppDbSet.ToList();
}
public IEnumerable<TEntity> Find(Func<TEntity, bool> predicate)
{
return AppDbSet.Where<TEntity>(predicate);
}
public TEntity Single(Func<TEntity, bool> predicate)
{
return AppDbSet.FirstOrDefault(predicate);
}
//Lastly, implement a validation method
public bool IsValid(TEntity entity)
{
if (AppDbSet.SingleOrDefault(x => x.Title == entity.Title) != null)
return false;
else
return true;
}
}
公共类存储库,其中tenty:class,ienty
{
private YourContext=新建YourContext();
私有数据库集AppDbSet;
公共存储库()
{
AppDbSet=context.Set();
}
//一对夫妇的方法检索数据。。。
公共列表GetAll()
{
返回AppDbSet.ToList();
}
公共IEnumerable Find(Func谓词)
{
返回AppDbSet.Where(谓词);
}
公共tenty Single(Func谓词)
{
返回AppDbSet.FirstOrDefault(谓词);
}
//最后,实现一个验证方法
公共bool有效(TEntity实体)
{
if(AppDbSet.SingleOrDefault(x=>x.Title==entity.Title)!=null)
返回false;
其他的
返回true;
}
}
按如下方式使用存储库:
Repository<Course> courseRepository = new Repository<Course>();
Course course = new Course();
course.Title = "Boring course";
Console.WriteLine(courseRepository.IsValid(course));
Repository-courseRepository=newrepository();
课程=新课程();
课程。Title=“枯燥的课程”;
Console.WriteLine(courseRepository.IsValid(course));
希望有帮助。所以您希望此方法能够根据正在验证的实体类型在任何
DbSet
中查找重复项?是的,这就是我试图做的。您是否总是使用Title
进行验证?是的,它只会使用Title进行验证。我可能遇到的唯一问题是ReferenceType类,因为它是一个抽象类
public class Repository<TEntity> where TEntity: class, IEntity
{
private YourContext context = new YourContext();
private DbSet<TEntity> AppDbSet;
public Repository()
{
AppDbSet = context.Set<TEntity>();
}
//a couple of method to retrieve data...
public List<TEntity> GetAll()
{
return AppDbSet.ToList();
}
public IEnumerable<TEntity> Find(Func<TEntity, bool> predicate)
{
return AppDbSet.Where<TEntity>(predicate);
}
public TEntity Single(Func<TEntity, bool> predicate)
{
return AppDbSet.FirstOrDefault(predicate);
}
//Lastly, implement a validation method
public bool IsValid(TEntity entity)
{
if (AppDbSet.SingleOrDefault(x => x.Title == entity.Title) != null)
return false;
else
return true;
}
}
Repository<Course> courseRepository = new Repository<Course>();
Course course = new Course();
course.Title = "Boring course";
Console.WriteLine(courseRepository.IsValid(course));