locked
Decimal Precision, scale RRS feed

Answers

  • User1449651191 posted

    I add the below line in context 

    modelBuilder.Entity<table>()
    .Property(s => s.ContractId).ForSqlServerHasColumnType("decimal(32,0)");

    and I added it to migration which created the below for me: 

    protected override void Up(MigrationBuilder migrationBuilder)
    {
    migrationBuilder.AddColumn<decimal>(
    name: "ContractId",
    table: "EmployeeProfile",
    type: "decimal(32,0)",
    nullable: false,
    defaultValue: 0m);
    }
    
    
    
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 20, 2017 8:34 PM

All replies

  • User-832373396 posted

    Hi dlmayahi,

    I tested an working example for specifying decimal (18,2).

    (note : i am using .net core 2.0)

    Here are the step in details:

    1:Add a Model called E

      public class E
        {
            public int Eid { get; set; }
            public decimal EMydecimal { get; set; }
        }

    2:specify .HasColumnType("decimal(18,2)") for EMydecimal column

     public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
        
        {
            public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
                : base(options)
            {
            }
        
       
       
              
            protected override void OnModelCreating(ModelBuilder builder)
            {
                base.OnModelCreating(builder);
    
                builder.Entity<E>().Property(propertyExpression: p => p.EMydecimal).HasColumnType("decimal(18,2)");
    
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);
           }          
               public DbSet<E> Es { get; set; }
           
        }

    3: find View->other Windows ->Package Manager Console

    4: type Add-migration fore

    next ,type update-database

    PM> Add-migration fore
    Both Entity Framework Core and Entity Framework 6 are installed. The Entity Framework Core tools are running. Use 'EntityFramework\Add-Migration' for Entity Framework 6.
    Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
          User profile is available. Using 'C:\Users\v-tiguo\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
    Microsoft.EntityFrameworkCore.Infrastructure[100403]
          Entity Framework Core 2.0.0-rtm-26452 initialized 'ApplicationDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
    To undo this action, use Remove-Migration.
    PM> update-database
    Both Entity Framework Core and Entity Framework 6 are installed. The Entity Framework Core tools are running. Use 'EntityFramework\Update-Database' for Entity Framework 6.
    Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
          User profile is available. Using 'C:\Users\v-tiguo\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
    Microsoft.EntityFrameworkCore.Infrastructure[100403]
          Entity Framework Core 2.0.0-rtm-26452 initialized 'ApplicationDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
    Microsoft.EntityFrameworkCore.Database.Command[200101]
          Executed DbCommand (72ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          SELECT OBJECT_ID(N'__EFMigrationsHistory');
    Microsoft.EntityFrameworkCore.Database.Command[200101]
          Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          SELECT OBJECT_ID(N'__EFMigrationsHistory');
    Microsoft.EntityFrameworkCore.Database.Command[200101]
          Executed DbCommand (37ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          SELECT [MigrationId], [ProductVersion]
          FROM [__EFMigrationsHistory]
          ORDER BY [MigrationId];
    Microsoft.EntityFrameworkCore.Migrations[200402]
          Applying migration '20171018064437_fore'.
    Applying migration '20171018064437_fore'.
    Microsoft.EntityFrameworkCore.Database.Command[200101]
          Executed DbCommand (30ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          DROP INDEX [UserNameIndex] ON [AspNetUsers];
    Microsoft.EntityFrameworkCore.Database.Command[200101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          DROP INDEX [IX_AspNetUserRoles_UserId] ON [AspNetUserRoles];
    Microsoft.EntityFrameworkCore.Database.Command[200101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          DROP INDEX [RoleNameIndex] ON [AspNetRoles];
    Microsoft.EntityFrameworkCore.Database.Command[200101]
          Executed DbCommand (79ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          CREATE TABLE [Es] (
              [Eid] int NOT NULL IDENTITY,
              [EMydecimal] decimal(18,2) NOT NULL,
              CONSTRAINT [PK_Es] PRIMARY KEY ([Eid])
          );
    Microsoft.EntityFrameworkCore.Database.Command[200101]
          Executed DbCommand (14ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          CREATE UNIQUE INDEX [UserNameIndex] ON [AspNetUsers] ([NormalizedUserName]) WHERE [NormalizedUserName] IS NOT NULL;
    Microsoft.EntityFrameworkCore.Database.Command[200101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          CREATE UNIQUE INDEX [RoleNameIndex] ON [AspNetRoles] ([NormalizedName]) WHERE [NormalizedName] IS NOT NULL;
    Microsoft.EntityFrameworkCore.Database.Command[200101]
          Executed DbCommand (13ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          ALTER TABLE [AspNetUserTokens] ADD CONSTRAINT [FK_AspNetUserTokens_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE;
    Microsoft.EntityFrameworkCore.Database.Command[200101]
          Executed DbCommand (55ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
          VALUES (N'20171018064437_fore', N'2.0.0-rtm-26452');
    Done.
    PM> 

    5. Ok ,you could find the column with type decimal (18,2) in database

    With regards,Angelina Jolie

    Wednesday, October 18, 2017 6:55 AM
  • User1449651191 posted

    I add the below line in context 

    modelBuilder.Entity<table>()
    .Property(s => s.ContractId).ForSqlServerHasColumnType("decimal(32,0)");

    and I added it to migration which created the below for me: 

    protected override void Up(MigrationBuilder migrationBuilder)
    {
    migrationBuilder.AddColumn<decimal>(
    name: "ContractId",
    table: "EmployeeProfile",
    type: "decimal(32,0)",
    nullable: false,
    defaultValue: 0m);
    }
    
    
    
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 20, 2017 8:34 PM