我有一个EF6的MVC应用程序。有没有办法自动将模型的所有属性设置为[Required]
?我们的一些模型很大,包含所有必填字段。有什么方法可以保存代码行或使其更清洁?
public class Employee{
[Required]
public string Name { get; set; }
[Required]
public string Address 1 { get; set; }
[Required]
public string Address 2 { get; set; }
[Required]
public int SSN { get; set; }
[Required]
public double PayRate { get; set; }
[Required]
public int PayType { get; set; }
[Required]
public string JobTitle { get; set; }
[Required]
public bool FullTime { get; set; }
[Required]
public string Sex { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Name { get; set; }
}
由于
答案 0 :(得分:2)
您可以通过Fluent API和"动态" MakeStringColumnsRequired
或MakeAllNullableColumnsRequired
Expression
S:
protected void MakeStringColumnsRequired<T>(DbModelBuilder modelBuilder)
where T : class
{
var type = typeof(T);
foreach (var prop in type.GetProperties().Where(x => x.PropertyType == typeof(string)))
{
var argParam = Expression.Parameter(type, "x");
var nameProperty = Expression.Property(argParam, prop.Name);
var lambda = Expression.Lambda<Func<T, string>>(nameProperty, argParam);
modelBuilder.Entity<T>().Property(lambda).IsRequired();
}
}
protected void MakeAllNullableColumnsRequired<T>(DbModelBuilder modelBuilder)
where T : class
{
var type = typeof(T);
foreach (var prop in type.GetProperties()
.Where(x => Nullable.GetUnderlyingType(x.PropertyType) != null || x.PropertyType == typeof(string))
)
{
var argParam = Expression.Parameter(type, "x");
var nameProperty = Expression.Property(argParam, prop.Name);
var funcType = typeof(Func<,>);
funcType = funcType.MakeGenericType(typeof(T), prop.PropertyType);
var lambdaMethod = typeof(Expression).GetMethods(BindingFlags.Public | BindingFlags.Static)
.Where(x => x.Name == "Lambda" && x.IsGenericMethod).First();
lambdaMethod = lambdaMethod.MakeGenericMethod(funcType);
var lambda = lambdaMethod.Invoke(null, new object[] { nameProperty, new ParameterExpression[] { argParam } });
var entity = modelBuilder.Entity<T>();
var entityPropertyMethods = entity.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Where(x => x.Name == "Property" && !x.IsGenericMethod).ToList();
var entityPropertyMethod = entityPropertyMethods
.Where(x => x.GetParameters().First().ParameterType.GetGenericArguments().First().GetGenericArguments().Last() == prop.PropertyType)
.FirstOrDefault();
if(entityPropertyMethod == null)
{
entityPropertyMethod = entity.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Where(x => x.Name == "Property" && x.IsGenericMethod).Last();
entityPropertyMethod = entityPropertyMethod.MakeGenericMethod(Nullable.GetUnderlyingType(prop.PropertyType));
}
var property = entityPropertyMethod.Invoke(entity, new object[] { lambda });
var isRequired = property.GetType().GetMethod("IsRequired");
isRequired.Invoke(property, null);
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
#if StringColumnsRequired
MakeStringColumnsRequired<Employee>(modelBuilder);
#else
MakeAllNullableColumnsRequired<Employee>(modelBuilder)
#endif
}