Answers:
Dave Van den Eynde的答案现在已过时。从EF 4.1开始,有2个重要的更改,ModelBuilder类现在是DbModelBuilder,现在有DecimalPropertyConfiguration.HasPrecision方法,其签名为:
public DecimalPropertyConfiguration HasPrecision(
byte precision,
byte scale )
其中precision是db将存储的总位数,而不管小数点在哪里,而scale是它将存储的小数位数。
因此,无需遍历所示的属性,而可以仅从中调用
public class EFDbContext : DbContext
{
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Class>().Property(object => object.property).HasPrecision(12, 10);
base.OnModelCreating(modelBuilder);
}
}
System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder
base.OnModelCreating(modelBuilder);
。这是故意的,还是只是在线而不是在IDE中键入代码的受害者?
如果要decimals
在EF6中设置所有精度,则可以替换DecimalPropertyConvention
在DbModelBuilder
:中使用的默认约定:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<DecimalPropertyConvention>();
modelBuilder.Conventions.Add(new DecimalPropertyConvention(38, 18));
}
DecimalPropertyConvention
EF6中的默认值将decimal
属性映射到decimal(18,2)
列。
如果仅希望单个属性具有指定的精度,则可以在上为实体的属性设置精度DbModelBuilder
:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>().Property(e => e.Value).HasPrecision(38, 18);
}
或者,EntityTypeConfiguration<>
为指定精度的实体添加:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new MyEntityConfiguration());
}
internal class MyEntityConfiguration : EntityTypeConfiguration<MyEntity>
{
internal MyEntityConfiguration()
{
this.Property(e => e.Value).HasPrecision(38, 18);
}
}
我很高兴为此创建一个自定义属性:
[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(PRECISION,SCALE);
所以我通过反射调用modelBuilder.Entity()并将其存储在EntityConfig变量中,然后构建“ c => c.PROPERTY_NAME” lambda表达式
之后,如果小数可为空,则调用
Property(Expression<Func<TStructuralType, decimal?>> propertyExpression)
方法(我通过数组中的位置来称呼它,我知道这不是理想的,任何帮助将不胜感激)
如果它不为空,我打电话给
Property(Expression<Func<TStructuralType, decimal>> propertyExpression)
方法。
让DecimalPropertyConfiguration我调用HasPrecision方法。
MethodInfo methodInfo = entityConfig.GetType().GetMethod("Property", new[] { lambdaExpression.GetType() });
用来获取正确的过载。到目前为止似乎仍然有效。
通过使用DecimalPrecisonAttribute
KinSlayerUY中的from,可以在EF6中创建一个约定,该约定将处理具有该属性的单个属性(而不是DecimalPropertyConvention
在此答案中设置like,这会影响所有十进制属性)。
[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; }
}
public class DecimalPrecisionAttributeConvention
: PrimitivePropertyAttributeConfigurationConvention<DecimalPrecisionAttribute>
{
public override void Apply(ConventionPrimitivePropertyConfiguration configuration, DecimalPrecisionAttribute attribute)
{
if (attribute.Precision < 1 || attribute.Precision > 38)
{
throw new InvalidOperationException("Precision must be between 1 and 38.");
}
if (attribute.Scale > attribute.Precision)
{
throw new InvalidOperationException("Scale must be between 0 and the Precision value.");
}
configuration.HasPrecision(attribute.Precision, attribute.Scale);
}
}
然后在您的DbContext
:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add(new DecimalPrecisionAttributeConvention());
}
Precision
,那么我建议将上限设置为28(> 28
以您的情况为准)。根据MSDN文档,System.Decimal
最多只能表示28-29位精度(msdn.microsoft.com/en-us/library/364x0z75.aspx)。同样,该属性声明Scale
为byte
,这意味着attribute.Scale < 0
不需要您的前提条件。
System.Decimal
事实并非如此。因此,将上限条件设置为大于28的值是没有意义的。System.Decimal
显然不能代表这么大的数字。另外,请注意,此属性对SQL Server以外的数据提供程序很有用。例如,PostgreSQL的numeric
类型最多支持131072位精度。
decimal(38,9)
列将很乐意持有,System.Decimal.MaxValue
但一decimal(28,9)
列则不会。没有理由将精度限制为仅
显然,您可以重写DbContext.OnModelCreating()方法并按以下方式配置精度:
protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().Property(product => product.Price).Precision = 10;
modelBuilder.Entity<Product>().Property(product => product.Price).Scale = 2;
}
但是,当您必须对所有与价格相关的属性进行处理时,这是非常繁琐的代码,因此我想出了这一点:
protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
{
var properties = new[]
{
modelBuilder.Entity<Product>().Property(product => product.Price),
modelBuilder.Entity<Order>().Property(order => order.OrderTotal),
modelBuilder.Entity<OrderDetail>().Property(detail => detail.Total),
modelBuilder.Entity<Option>().Property(option => option.Price)
};
properties.ToList().ForEach(property =>
{
property.Precision = 10;
property.Scale = 2;
});
base.OnModelCreating(modelBuilder);
}
优良作法是在覆盖方法时调用基本方法,即使基本实现不执行任何操作。
更新:本文也非常有帮助。
base.OnModelCreating(modelBuilder);
必要打来电话。从VS中的DbContext元数据中: The default implementation of this method does nothing, but it can be overridden in a derived class such that the model can be further configured before it is locked down.
实体框架版本6(Alpha,rc1)具有称为“ 自定义约定”的名称。设置小数精度:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Properties<decimal>().Configure(config => config.HasPrecision(18, 4));
}
参考:
[Column(TypeName = "decimal(18,2)")]
这将作为描述EF核心代码首先迁移工作在这里。
The store type 'decimal(18,2)' could not be found in the SqlServer provider manifest
- EF的核心- 与使用System.ComponentModel.DataAnnotations;
使用 [Column
(TypeName
= "decimal
(精度,刻度)")]
精度 = 使用的字符总数
比例 = 点后的总数。(容易混淆)
范例:
public class Blog
{
public int BlogId { get; set; }
[Column(TypeName = "varchar(200)")]
public string Url { get; set; }
[Column(TypeName = "decimal(5, 2)")]
public decimal Rating { get; set; }
}
此处有更多详细信息:https : //docs.microsoft.com/zh-cn/ef/core/modeling/relational/data-types
在EF6中
modelBuilder.Properties()
.Where(x => x.GetCustomAttributes(false).OfType<DecimalPrecisionAttribute>().Any())
.Configure(c => {
var attr = (DecimalPrecisionAttribute)c.ClrPropertyInfo.GetCustomAttributes(typeof (DecimalPrecisionAttribute), true).FirstOrDefault();
c.HasPrecision(attr.Precision, attr.Scale);
});
您总是可以告诉EF使用OnModelCreating函数的Context类中的约定来执行此操作,如下所示:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// <... other configurations ...>
// modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
// modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
// modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
// Configure Decimal to always have a precision of 18 and a scale of 4
modelBuilder.Conventions.Remove<DecimalPropertyConvention>();
modelBuilder.Conventions.Add(new DecimalPropertyConvention(18, 4));
base.OnModelCreating(modelBuilder);
}
这仅适用于Code First EF fyi,并且适用于映射到数据库的所有十进制类型。
Remove<DecimalPropertyConvention>();
之前才起作用Add(new DecimalPropertyConvention(18, 4));
。我认为奇怪的是,它不会自动被覆盖。
使用
System.ComponentModel.DataAnnotations;
您可以简单地将该属性放入模型中:
[DataType("decimal(18,5)")]
您可以在MSDN上找到更多信息-实体数据模型的方面。 http://msdn.microsoft.com/zh-cn/library/ee382834.aspx 完整推荐。
实际对于EntityFrameworkCore 3.1.3:
OnModelCreating中的一些解决方案:
var fixDecimalDatas = new List<Tuple<Type, Type, string>>();
foreach (var entityType in builder.Model.GetEntityTypes())
{
foreach (var property in entityType.GetProperties())
{
if (Type.GetTypeCode(property.ClrType) == TypeCode.Decimal)
{
fixDecimalDatas.Add(new Tuple<Type, Type, string>(entityType.ClrType, property.ClrType, property.GetColumnName()));
}
}
}
foreach (var item in fixDecimalDatas)
{
builder.Entity(item.Item1).Property(item.Item2, item.Item3).HasColumnType("decimal(18,4)");
}
//custom decimal nullable:
builder.Entity<SomePerfectEntity>().Property(x => x.IsBeautiful).HasColumnType("decimal(18,4)");
KinSlayerUY的自定义属性对我来说效果很好,但是我遇到了ComplexTypes问题。它们在属性代码中被映射为实体,因此无法被映射为ComplexType。
因此,我扩展了代码以允许这样做:
public static void OnModelCreating(DbModelBuilder modelBuilder)
{
foreach (Type classType in from t in Assembly.GetAssembly(typeof(DecimalPrecisionAttribute)).GetTypes()
where t.IsClass && t.Namespace == "FA.f1rstval.Data"
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) }))
{
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;
int MethodNum;
if (propAttr.prop.PropertyType.IsGenericType && propAttr.prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
MethodNum = 7;
}
else
{
MethodNum = 6;
}
//check if complextype
if (classType.GetCustomAttribute<ComplexTypeAttribute>() != null)
{
var complexConfig = modelBuilder.GetType().GetMethod("ComplexType").MakeGenericMethod(classType).Invoke(modelBuilder, null);
MethodInfo methodInfo = complexConfig.GetType().GetMethods().Where(p => p.Name == "Property").ToList()[MethodNum];
decimalConfig = methodInfo.Invoke(complexConfig, new[] { lambdaExpression }) as DecimalPropertyConfiguration;
}
else
{
var entityConfig = modelBuilder.GetType().GetMethod("Entity").MakeGenericMethod(classType).Invoke(modelBuilder, null);
MethodInfo methodInfo = entityConfig.GetType().GetMethods().Where(p => p.Name == "Property").ToList()[MethodNum];
decimalConfig = methodInfo.Invoke(entityConfig, new[] { lambdaExpression }) as DecimalPropertyConfiguration;
}
decimalConfig.HasPrecision(propAttr.attr.Precision, propAttr.attr.Scale);
}
}
}
@ Mark007,我已更改类型选择条件以使用DbContext的DbSet <>属性。我认为这样做比较安全,因为有时在给定名称空间中有一些类不应属于模型定义,或者它们不是实体。或者,您的实体可以驻留在单独的名称空间或单独的程序集中,并一起放入一个Context中。
另外,尽管不太可能,但我认为依赖方法定义的顺序并不安全,因此最好通过“参数”列表将它们拉出。(.GetTypeMethods()是我为与新的TypeInfo范例一起使用而构建的扩展方法,在查找方法时可以展平类层次结构)。
请注意,OnModelCreating委托了此方法:
private void OnModelCreatingSetDecimalPrecisionFromAttribute(DbModelBuilder modelBuilder)
{
foreach (var iSetProp in this.GetType().GetTypeProperties(true))
{
if (iSetProp.PropertyType.IsGenericType
&& (iSetProp.PropertyType.GetGenericTypeDefinition() == typeof(IDbSet<>) || iSetProp.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>)))
{
var entityType = iSetProp.PropertyType.GetGenericArguments()[0];
foreach (var propAttr in entityType
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Select(p => new { prop = p, attr = p.GetCustomAttribute<DecimalPrecisionAttribute>(true) })
.Where(propAttr => propAttr.attr != null))
{
var entityTypeConfigMethod = modelBuilder.GetType().GetTypeInfo().DeclaredMethods.First(m => m.Name == "Entity");
var entityTypeConfig = entityTypeConfigMethod.MakeGenericMethod(entityType).Invoke(modelBuilder, null);
var param = ParameterExpression.Parameter(entityType, "c");
var lambdaExpression = Expression.Lambda(Expression.Property(param, propAttr.prop.Name), true, new ParameterExpression[] { param });
var propertyConfigMethod =
entityTypeConfig.GetType()
.GetTypeMethods(true, false)
.First(m =>
{
if (m.Name != "Property")
return false;
var methodParams = m.GetParameters();
return methodParams.Length == 1 && methodParams[0].ParameterType == lambdaExpression.GetType();
}
);
var decimalConfig = propertyConfigMethod.Invoke(entityTypeConfig, new[] { lambdaExpression }) as DecimalPropertyConfiguration;
decimalConfig.HasPrecision(propAttr.attr.Precision, propAttr.attr.Scale);
}
}
}
}
public static IEnumerable<MethodInfo> GetTypeMethods(this Type typeToQuery, bool flattenHierarchy, bool? staticMembers)
{
var typeInfo = typeToQuery.GetTypeInfo();
foreach (var iField in typeInfo.DeclaredMethods.Where(fi => staticMembers == null || fi.IsStatic == staticMembers))
yield return iField;
//this bit is just for StaticFields so we pass flag to flattenHierarchy and for the purpose of recursion, restrictStatic = false
if (flattenHierarchy == true)
{
var baseType = typeInfo.BaseType;
if ((baseType != null) && (baseType != typeof(object)))
{
foreach (var iField in baseType.GetTypeMethods(true, staticMembers))
yield return iField;
}
}
}
[Column(TypeName = "decimal(18,4)")]
属性用作十进制属性