实体框架核心 - 将十进制精度和比例设置为所有十进制属性 [英] Entity Framework Core - setting the decimal precision and scale to all decimal properties
本文介绍了实体框架核心 - 将十进制精度和比例设置为所有十进制属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将所有十进制属性的精度设置为(18,6)。在EF6中,这很简单:
I want to set the precision of all the decimal properties to (18,6). In EF6 this was quite easy:
modelBuilder.Properties<decimal>().Configure(x => x.HasPrecision(18, 6));
但我似乎在EF Core中找不到类似的东西。删除级联删除约定不像EF6那么简单,所以我发现了以下解决方法:
but I can't seem to find anything similar to this in EF Core. Removing the cascade delete convention wasn't as simple as in EF6 so I found the following workaround:
EF6:
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
EF核心:
foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
relationship.DeleteBehavior = DeleteBehavior.Restrict;
之后,我阅读,我尝试了一个类似的方法:
and after I read this, I tried a similar approach:
foreach (var entityType in modelBuilder.Model.GetEntityTypes()
.SelectMany(x => x.GetProperties())
.Where(x => x.ClrType == typeof(decimal)))
{
// what to do here?
}
我希望如果我在正确的轨道上,如何继续,或如果没有,我应该开始在所有十进制
属性中添加数据注释。
I would like if I am on the right track and how to continue, or if not, should I start putting data annotations on all the decimal
properties.
推荐答案
你已经很近了这是代码。
You got close. Here's the code.
foreach (var property in modelBuilder.Model.GetEntityTypes()
.SelectMany(t => t.GetProperties())
.Where(p => p.ClrType == typeof(decimal)))
{
property.Relational().ColumnType = "decimal(18, 6)";
}
这篇关于实体框架核心 - 将十进制精度和比例设置为所有十进制属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文