entity framework核心 – 将小数精度和比例设置为所有小数属性

我想将所有十进制属性的精度设置为(18,6)。 在EF6中,这非常简单:

modelBuilder.Properties().Configure(x => x.HasPrecision(18, 6)); 

但我似乎无法在EF Core中找到类似的东西。 删除级联删除约定并不像在EF6中那么简单,因此我找到了以下解决方法:

EF6:

 modelBuilder.Conventions.Remove(); modelBuilder.Conventions.Remove(); 

EF核心:

 foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys())) relationship.DeleteBehavior = DeleteBehavior.Restrict; 

在我读完之后,我尝试了类似的方法:

 foreach (var entityType in modelBuilder.Model.GetEntityTypes() .SelectMany(x => x.GetProperties()) .Where(x => x.ClrType == typeof(decimal))) { // what to do here? } 

我想如果我在正确的轨道上以及如何继续,或者如果没有,我应该开始在所有decimal属性上放置数据注释。

你很亲密 这是代码。

 foreach (var property in modelBuilder.Model.GetEntityTypes() .SelectMany(t => t.GetProperties()) .Where(p => p.ClrType == typeof(decimal))) { property.Relational().ColumnType = "decimal(18, 6)"; }