这是我在这里发布的同一问题的答案,链接为:
https://dev59.com/o3A75IYBdhLWcg3wB0ZP#15386883。
我很愉快地为此创建了一个自定义属性:
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public sealed class DecimalPrecisionAttribute : Attribute
{
public DecimalPrecisionAttribute(byte precision, byte scale)
{
Precision = precision;
Scale = scale;
}
public byte Precision { get; set; }
public byte Scale { get; set; }
}
像这样使用
[DecimalPrecision(20,10)]
public Nullable<decimal> DeliveryPrice { get; set; }
在模型创建时使用一些反射就可以实现魔法。
protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
{
foreach (Type classType in from t in Assembly.GetAssembly(typeof(DecimalPrecisionAttribute)).GetTypes()
where t.IsClass && t.Namespace == "YOURMODELNAMESPACE"
select t)
{
foreach (var propAttr in classType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.GetCustomAttribute<DecimalPrecisionAttribute>() != null).Select(
p => new { prop = p, attr = p.GetCustomAttribute<DecimalPrecisionAttribute>(true) }))
{
var entityConfig = modelBuilder.GetType().GetMethod("Entity").MakeGenericMethod(classType).Invoke(modelBuilder, null);
ParameterExpression param = ParameterExpression.Parameter(classType, "c");
Expression property = Expression.Property(param, propAttr.prop.Name);
LambdaExpression lambdaExpression = Expression.Lambda(property, true,
new ParameterExpression[]
{param});
DecimalPropertyConfiguration decimalConfig;
if (propAttr.prop.PropertyType.IsGenericType && propAttr.prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
MethodInfo methodInfo = entityConfig.GetType().GetMethods().Where(p => p.Name == "Property").ToList()[7];
decimalConfig = methodInfo.Invoke(entityConfig, new[] { lambdaExpression }) as DecimalPropertyConfiguration;
}
else
{
MethodInfo methodInfo = entityConfig.GetType().GetMethods().Where(p => p.Name == "Property").ToList()[6];
decimalConfig = methodInfo.Invoke(entityConfig, new[] { lambdaExpression }) as DecimalPropertyConfiguration;
}
decimalConfig.HasPrecision(propAttr.attr.Precision, propAttr.attr.Scale);
}
}
}
第一部分是获取模型中的所有类(我的自定义属性在该程序集中定义,因此我使用它来获取带有模型的程序集)
第二个 foreach 获取具有自定义属性的该类中的所有属性和属性本身,以便我可以获取精度和比例数据
之后我必须调用
modelBuilder.Entity<MODEL_CLASS>().Property(c=> c.PROPERTY_NAME).HasPrecision(PRECITION,SCALE);
我通过反射调用modelBuilder.Entity()并将其存储在entityConfig变量中,然后构建“c => c.PROPERTY_NAME”lambda表达式。
之后,如果小数可为空,我会调用
Property(Expression<Func<TStructuralType, decimal?>> propertyExpression)
方法(我通过数组中的位置调用此方法,我知道这不是理想的方式,任何帮助将不胜感激)
如果它不可为空,我调用
Property(Expression<Func<TStructuralType, decimal>> propertyExpression)
方法。
有了DecimalPropertyConfiguration之后,我调用HasPrecision方法。
OnModelCreating
方法(如上所示)之后,如果您在程序包管理器控制台中运行add-migration
命令,则它将识别您的新代码,并添加一个迁移以正确修改列。 - Ross Brasseaux