如何在不使用属性的情况下指定小数精度是最好的方法。 我只需要在我的Data.Models中为所有小数设置一次即可。每次都指定属性很麻烦。
public class Customer
{
public int customerId { get; set; }
[Column(TypeName = "decimal(18,2)")]
public decimal AvailableAmount { get; set; }
}
如何在不使用属性的情况下指定小数精度是最好的方法。 我只需要在我的Data.Models中为所有小数设置一次即可。每次都指定属性很麻烦。
public class Customer
{
public int customerId { get; set; }
[Column(TypeName = "decimal(18,2)")]
public decimal AvailableAmount { get; set; }
}
protected override void OnModelCreating(ModelBuilder builder)
{
foreach (var property in builder.Model.GetEntityTypes()
.SelectMany(t => t.GetProperties())
.Where(p => p.ClrType == typeof(decimal) || p.ClrType == typeof(decimal?)))
{
property.Relational().ColumnType = "decimal(18,2)";
}
}
对于在 EntityFrameworkCore 6.0 上遇到同样问题的人,可以尝试如下解决方案:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var decimalProps = modelBuilder.Model
.GetEntityTypes()
.SelectMany(t => t.GetProperties())
.Where(p => (System.Nullable.GetUnderlyingType(p.ClrType) ?? p.ClrType) == typeof(decimal));
foreach (var property in decimalProps)
{
property.SetPrecision(18);
property.SetScale(2);
}
}
我在使用ASP.NET Core 5时遇到了这个问题。我将以下代码添加到DbContext中的OnModelCreating方法中。
protected override void OnModelCreating(ModelBuilder modelBuilder)// Crée la migration
{
modelBuilder.Entity<MyEntity>().Property(p => p.Prix).HasColumnType("decimal(18,4)");
}
一切都开始正常工作了。
以下是示例:
[Column(TypeName = "decimal(18,2)")]
public decimal Quantity { get; set; }
可以这样定义:
[Precision(18, 2)]
public decimal Quantity { get; set; }
使用 EF Core 6 进行测试。
[Column(TypeName = "decimal(18,2)")]
public decimal Quantity { get; set; }
2- 使用精度
[Precision(18, 2)]
public decimal Quantity { get; set; }
3- 使用PropertyBuilder.HasPrecision 方法
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>().Property(p => p.MyProperty).HasPrecision(18, 2);
}
4- 使用 PropertyBuilder.HasColumnType 方法
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>().Property(p => p.MyProperty).HasColumnType("decimal(18,2)");
}
5- 使用IEntityTypeConfiguration 接口
public class MyEntityConfiguration : IEntityTypeConfiguration<MyEntity>
{
public void Configure(EntityTypeBuilder<MyEntity> builder)
{
builder.ToTable("MyEntity");
builder.Property(x => x.MyEntity).HasPrecision(18, 2);
}
}
然后
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(GetType().Assembly);
// Or use explicitly
//modelBuilder.ApplyConfiguration(new MyEntityConfiguration());
}
所有这些情况都将被忽略。
但是这段代码会关注它们并检查是否没有相关的注释,如果没有,它将添加到属性中。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var decimalProps = modelBuilder.Model
.GetEntityTypes()
.SelectMany(t => t.GetProperties())
.Where(p => (System.Nullable.GetUnderlyingType(p.ClrType) ?? p.ClrType) == typeof(decimal));
foreach (var property in decimalProps)
{
var annotations = property.GetAnnotations();
if (annotations.Count(x => x.Name is "Relational:ColumnType" or "Precision" or "Scale") != 0) continue;
// Or Use this line if you use older version of C#
// if (annotations.Count(x => x.Name == "Relational:ColumnType" || x.Name == "Precision" || x.Name == "Scale") != 0) continue;
property.SetPrecision(18);
property.SetScale(2);
}
}
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
var properties = entityType.ClrType.GetProperties().Where(p => p.PropertyType == typeof(decimal));
foreach (var property in properties)
{
modelBuilder.Entity(entityType.Name).Property(property.Name).HasColumnType("decimal(18,2)");
}
}
http://jameschambers.com/2019/06/No-Type-Was-Specified-for-the-Decimal-Column/
[Precision()]
属性来指定属性的不同精度,并且对所有实体上的decimal
属性都生效。protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Fetch all entity classes that derive from the `BaseEntity`
var entities = typeof(BaseEntity).Assembly.GetTypes()
.Where(t => t.IsSubclassOf(typeof(BaseEntity)) && !t.IsAbstract);
foreach (var entity in entities)
{
// Iterate thru the properties of the entity
foreach (var property in entity.GetProperties())
{
// If decimal or nullable decimal
// and not using the `Precision` attribtue, define the precision
if ((property.PropertyType == typeof(decimal)
|| property.PropertyType == typeof(decimal?))
&& !property.GetCustomAttributes<PrecisionAttribute>().Any())
{
modelBuilder.Entity(entity)
.Property(property.PropertyType, property.Name).HasPrecision(18, 2);
}
}
}
}
Microsoft.EntityFrameworkCore.Relational
包。 - vahid tajari