Gre*_*eMD 31 c# entity-framework entity-framework-core asp.net-core
我想将所有十进制属性的精度设置为(18,6).在EF6中,这非常简单:
modelBuilder.Properties<decimal>().Configure(x => x.HasPrecision(18, 6));
Run Code Online (Sandbox Code Playgroud)
但我似乎无法在EF Core中找到类似的东西.删除级联删除约定并不像在EF6中那么简单,因此我找到了以下解决方法:
EF6:
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
Run Code Online (Sandbox Code Playgroud)
EF核心:
foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
relationship.DeleteBehavior = DeleteBehavior.Restrict;
Run Code Online (Sandbox Code Playgroud)
我看后这个,我尝试了类似的方法:
foreach (var entityType in modelBuilder.Model.GetEntityTypes()
.SelectMany(x => x.GetProperties())
.Where(x => x.ClrType == typeof(decimal)))
{
// what to do here?
}
Run Code Online (Sandbox Code Playgroud)
我想如果我在正确的轨道上以及如何继续,或者如果没有,我应该开始在所有decimal属性上放置数据注释.
bri*_*lam 60
你很亲密 这是代码.
foreach (var property in modelBuilder.Model.GetEntityTypes()
.SelectMany(t => t.GetProperties())
.Where(p => p.ClrType == typeof(decimal)))
{
// EF Core 1 & 2
property.Relational().ColumnType = "decimal(18, 6)";
// EF Core 3
//property.SetColumnType("decimal(18, 6)");
}
Run Code Online (Sandbox Code Playgroud)
注意:如果您有可空类型decimal?,请使用:
Where(p => p.ClrType == typeof(decimal) || p.ClrType == typeof(decimal?))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17199 次 |
| 最近记录: |