十进制precision和规模EF code首先 [英] Decimal precision and scale in EF Code First

查看:291
本文介绍了十进制precision和规模EF code首先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这个code-第一种方法实验,但我现在发现,System.Decimal类型的属性被映射到decimal类型的SQL列(18,0)。

I'm experimenting with this code-first approach, but I'm find out now that a property of type System.Decimal gets mapped to a sql column of type decimal(18, 0).

我如何设置数据库列的precision?

How do I set the precision of the database column?

推荐答案

来自Dave范登Eynde答案是已经过时。有2个重要的变化,从4.1 EF起,模型构建器类现在<一个href=\"http://msdn.microsoft.com/en-us/library/system.data.entity.modelconfiguration.configuration.decimalpropertyconfiguration.has$p$pcision%28v=vs.103%29.aspx\">DbModelBuilder现在还拥有的签名DecimalPropertyConfiguration.Has precision方式:

The answer from Dave Van den Eynde is now out of date. There are 2 important changes, from EF 4.1 onwards the ModelBuilder class is now DbModelBuilder and there is now a DecimalPropertyConfiguration.HasPrecision Method which has a signature of:

public DecimalPropertyConfiguration HasPrecision(
byte precision,
byte scale )

,其中precision是数字的DB存储的总数,无论在哪里小数点落在和规模是小数它将存储的数量。

where precision is the total number of digits the db will store, regardless of where the decimal point falls and scale is the number of decimal places it will store.

因此​​没有必要通过属性来迭代如图但可以只从

Therefore there is no need to iterate through properties as shown but the can just be called from

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);
   }
}

这篇关于十进制precision和规模EF code首先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆