问题描述
我的模型中可能有几十个小数属性,我在 OnModelCreating 中设置精度如下:
modelBuilder.Entity<Models.MyModel>()
.Property(x => x.MyProp)
.HasPrecision(18,2);
但是长度已经失控了,所以我想知道是否有办法遍历上下文中的每个小数并设置精度 (18,2)?
解决方法
流畅的验证。 用于构建强类型验证规则的流行 .NET 库。 看看这个,希望这能解决你的问题。
,EF 允许您从 EntityTypeConfiguration
继承并使用它为每个实体进行自己的配置。然后将 EntityTypeConfiguration-derived
类添加到 fluent set。
例如:书籍配置类
public class BookConfig : EntityTypeConfiguration<Book>
{
public BookConfig()
{
Property(p => p.bookNo).HasPrecision(18,2);
Property(p => p.BookPrice).HasPrecision(18,2);
}
}
现在介绍一下fluent库
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new BookConfig());
}