我正在试验这种代码优先的方法,但我现在发现类型System的属性。Decimal被映射到Decimal(18,0)类型的sql列。
如何设置数据库列的精度?
我正在试验这种代码优先的方法,但我现在发现类型System的属性。Decimal被映射到Decimal(18,0)类型的sql列。
如何设置数据库列的精度?
当前回答
您可以在实体数据模型的MSDN方面找到更多信息。 http://msdn.microsoft.com/en-us/library/ee382834.aspx 完整的建议。
其他回答
Dave Van den Eynde给出的答案现在已经过时了。有2个重要的变化,从EF 4.1开始,ModelBuilder类现在是DbModelBuilder,现在有一个DecimalPropertyConfiguration。HasPrecision方法,其签名为:
public DecimalPropertyConfiguration HasPrecision(
byte precision,
byte scale )
其中精度是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);
}
}
我有一个很好的时间为这个创建自定义属性:
[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方法。
你总是可以告诉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,并适用于映射到db的所有十进制类型。
这就是我正在寻找的,适用于普通的MVC项目(没有。net核心)
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<YOUR_CLASS_NAME>().Property(x => x.YOUR_DECIAML_PROP).HasPrecision(18, 6);
base.OnModelCreating(modelBuilder);
}
}
包管理器控制台
add-migration changeDecimalPrecision
生成的迁移
public override void Up()
{
AlterColumn("dbo.YOUR_CLASS_NAME", "YOUR_DECIAML_PROP", c => c.Decimal(nullable: false, precision: 18, scale: 6));
}
如果你想在EF6中设置所有小数的精度,你可以替换DbModelBuilder中使用的默认DecimalPropertyConvention约定:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<DecimalPropertyConvention>();
modelBuilder.Conventions.Add(new DecimalPropertyConvention(38, 18));
}
EF6中的默认DecimalPropertyConvention将十进制属性映射为十进制(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);
}
}