gpt4 book ai didi

c# - MVC 模型需要所有字段

转载 作者:太空狗 更新时间:2023-10-29 23:36:13 26 4
gpt4 key购买 nike

我有一个带有 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; }
}

谢谢

最佳答案

您可以通过 Fluent API 和“动态”表达式MakeStringColumnsRequiredMakeAllNullableColumnsRequired:

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
}

关于c# - MVC 模型需要所有字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49760080/

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