我正在试验这种代码优先的方法,但我现在发现类型System的属性。Decimal被映射到Decimal(18,0)类型的sql列。
如何设置数据库列的精度?
我正在试验这种代码优先的方法,但我现在发现类型System的属性。Decimal被映射到Decimal(18,0)类型的sql列。
如何设置数据库列的精度?
当前回答
我有一个很好的时间为这个创建自定义属性:
[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方法。
其他回答
编辑,从。net 6开始,这可以用precision属性代替
(精密(精度、规模))
EF Core之前的版本:
[列(TypeName = "十进制(精度,比例)")]
定义:
精度=使用的字符总数
刻度=点后的总数。(容易混淆)
例子:
using System.ComponentModel.DataAnnotations; //.Net Core
using Microsoft.EntityFrameworkCore; //.NET 6+
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; }
[Precision(28, 8)]
public decimal RatingV6 { get; set; }
}
更多详情请访问:https://learn.microsoft.com/en-us/ef/core/modeling/relational/data-types
我有一个很好的时间为这个创建自定义属性:
[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方法。
使用KinSlayerUY的DecimalPrecisonAttribute,在EF6中,你可以创建一个惯例,它将处理具有该属性的单个属性(而不是像这个答案中那样设置DecimalPropertyConvention,这会影响所有的十进制属性)。
[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());
}
实体框架6 (Alpha, rc1)有一些叫做自定义约定的东西。设置十进制精度。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Properties<decimal>().Configure(config => config.HasPrecision(18, 4));
}
参考:
http://entityframework.codeplex.com/wikipage?title=Custom%20Conventions http://weblogs.asp.net/scottgu/archive/2012/12/11/entity-framework-6-alpha2-now-available.aspx
从。net EF Core 6开始,你可以使用Precision属性。
[Precision(18, 2)]
public decimal Price { get; set; }
请确保您需要安装EF Core 6,并使用line进行以下操作
using Microsoft.EntityFrameworkCore;