Open Closed

Apply decimal precision generic convention for all entities not working for EFCore v7.0.1 #5202


User avatar
0
younesraweh.s2d@gmail.com created

We need to apply generic convention by implementing IModelFinalizingConvention interface and adding new class to overrided DbContext function ConfigureConventions but when run add migration command nothing happened which means the convention is not applied on our decimal properties. did we miss any config or there is another way to apply generic convention for all entities all decimal properties.

Note: We applied the same steps in another dotnet 7 project (non Abp project ) and convention working as expected.
  • ABP Framework version: v7.2.2
  • UI type: Angular
  • DB provider: EF Core v7.0.1
  • project type : module project
  • Steps to reproduce the issue:"
  • 1- add new class for convention as below
    public class DecimalPrecisionConvention : IModelFinalizingConvention
    {
    public void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext\<IConventionModelBuilder> context)
    {
    foreach (var property in modelBuilder.Metadata.GetEntityTypes()
    .SelectMany(entityType => entityType.GetDeclaredProperties()
    .Where(property => property.ClrType == typeof(decimal))))
    {
    
    property.Builder.HasColumnType("decimal(18,6)");
    }
    }
    }
  • 2- override ConfigureConventions function for DbContext as below
 protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
    {
    configurationBuilder.Conventions.Add(\_ => new DecimalPrecisionConvention());
    }

2 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    It works for me.

    My steps:

    create a new project: abp new Qa

    public class DecimalPrecisionConvention : IModelFinalizingConvention
    {
        public void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder> context)
        {
            foreach (var property in modelBuilder.Metadata.GetEntityTypes()
                         .SelectMany(entityType => entityType.GetDeclaredProperties()
                             .Where(property => property.ClrType == typeof(decimal))))
            {
                property.Builder.HasColumnType("decimal(18,6)");
            }
        }
    }
    
    
    public class Book : AggregateRoot<Guid>
    {
        public string Name { get; set; }
    
        public decimal Price { get; set; }
    }
    
    
    public DbSet<Book> Books { get; set; }
    
    protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
    {
        configurationBuilder.Conventions.Add(_ => new DecimalPrecisionConvention());
    }
    
    protected override void OnModelCreating(ModelBuilder builder)
    {
        ......
        builder.Entity<Book>(b =>
        {
            b.ToTable(QaConsts.DbTablePrefix + "Books");
            b.ConfigureByConvention();
        });
    }
    

  • User Avatar
    0
    younesraweh.s2d@gmail.com created

    It worked after moving it to API host module DbContext class so thank you very much

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09