简体   繁体   中英

Incorrect Decimal precision and scale when creating database with Code First in Entity Framework 6.1

I have a model that inherits from IdentityDbContext in my project, created from the project template of ASP.NET MVC created with Visual Studio 2013 Update 2.

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

        }
    }

I need to change the default decimal precision in the database when it is created, so I found in other posts that it should be done in OnModelCreating, just as I did. Everything seems to work fine, but, when the database is created, the decimal fields gets the default precision and scale of decimal(18,2). I have seen in other posts that the model inherits from DBContext, instead of IdentityDbContext, I don't know if there is something I need to do to get it working. I'm using localdb in Visual Studio 2013. Thanks in regards to anyone that could help me.

I solved the problem. Happens that Visual Studio creates a class ApplicationDbContext as part of the project that already inherits from IdentityDbContext, so the MyModel class should inherits from this class or I should set the decimal properties in the OnModelCreating of ApplicationDbContext class:

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

        }
    }

Both ways works fine!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM