在Entity Framework 6.1中使用Code First创建数据库时,十进制精度和比例不正确

时间:2014-07-23 13:10:09

标签: c# asp.net-mvc entity-framework ef-code-first localdb

我有一个模型,它继承自我的项目中的IdentityDbContext,是从使用Visual Studio 2013 Update 2创建的ASP.NET MVC的项目模板创建的。

public partial class MyModel : IdentityDbContext<ApplicationUser>
    {
        public MyModel()
            : base("DefaultConnection")
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyModel>());
        }
        //....other properties

        public virtual DbSet<ProductClientDiscount> ProductClientPrice { get; set; }
        public virtual DbSet<ClientCategoryDiscount> ClientCategoryDiscount { get; set; }
        public virtual DbSet<Product> Products { get; set; }
        //....other properties


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);


           modelBuilder.Entity<ClientCategoryDiscount>().Property(x => x.PercentDiscountRent).HasPrecision(12, 9);
            modelBuilder.Entity<ClientCategoryDiscount>().Property(x => x.PercentDiscountSale).HasPrecision(12, 9);
            modelBuilder.Entity<ProductClientDiscount>().Property(x => x.PercentDiscountRent).HasPrecision(12, 9);
            modelBuilder.Entity<ProductClientDiscount>().Property(x => x.PercentDiscountSale).HasPrecision(12, 9);
            modelBuilder.Entity<Product>().Property(x => x.SalePrice).HasPrecision(12, 9);
            modelBuilder.Entity<Product>().Property(x => x.RentPrice).HasPrecision(12, 9);

        }
    }

我需要在创建数据库时更改数据库中的默认小数精度,所以我在其他帖子中发现它应该在OnModelCreating中完成,就像我一样。一切似乎都运行正常,但是,创建数据库时,十进制字段获取默认精度和小数位数(18,2)。我在其他帖子中看到模型继承自DBContext,而不是IdentityDbContext,我不知道是否需要做些什么才能让它工作。我在Visual Studio 2013中使用localdb。感谢任何可以帮助我的人。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。发现Visual Studio创建了一个类 ApplicationDbContext 作为已经从IdentityDbContext继承的项目的一部分,因此MyModel类应该继承自此类,或者我应该在 ApplicationDbContext的OnModelCreating中设置小数属性类:

public partial class MyModel : ApplicationDbContext
    {
        public MyModel()
            : base("DefaultConnection")
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyModel>());
        }
        //....other properties

        public virtual DbSet<ProductClientDiscount> ProductClientPrice { get; set; }
        public virtual DbSet<ClientCategoryDiscount> ClientCategoryDiscount { get; set; }
        public virtual DbSet<Product> Products { get; set; }
        //....other properties


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);


           modelBuilder.Entity<ClientCategoryDiscount>().Property(x => x.PercentDiscountRent).HasPrecision(12, 9);
            modelBuilder.Entity<ClientCategoryDiscount>().Property(x => x.PercentDiscountSale).HasPrecision(12, 9);
            modelBuilder.Entity<ProductClientDiscount>().Property(x => x.PercentDiscountRent).HasPrecision(12, 9);
            modelBuilder.Entity<ProductClientDiscount>().Property(x => x.PercentDiscountSale).HasPrecision(12, 9);
            modelBuilder.Entity<Product>().Property(x => x.SalePrice).HasPrecision(12, 9);
            modelBuilder.Entity<Product>().Property(x => x.RentPrice).HasPrecision(12, 9);

        }
    }

两种方式都可以正常使用!