1
How to create a formatting with precision 10,4 (4 decimal places) to save decimal data to SQL Server? This format description goes inside the modelBuilder, however, I have not found any code already defined to do this formatting.
1
How to create a formatting with precision 10,4 (4 decimal places) to save decimal data to SQL Server? This format description goes inside the modelBuilder, however, I have not found any code already defined to do this formatting.
2
Use the HasPrecision
:
public class EFDbContext : DbContext
{
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Class>()
.Property(object => object.property)
.HasPrecision(10, 4);
base.OnModelCreating(modelBuilder);
}
}
See more details about setting up property mappings using Fluent API in documentation.
2
If you need to set a decimal property, use:
modelBuilder.Entity<Class>(entity =>
{
entity.Property(e => e.PropertyName)
.HasColumnType("decimal(10,2)");
});
Browser other questions tagged c# sql-server entity-framework fluent-api
You are not signed in. Login or sign up in order to post.
Thank you, George. The solution is this. However, an error appears, identified exactly in the reserved word ". Hasprecision". The following error is: "Error: CS1061 - ?Propertybuilder<double>' does not contain a definition for "Hasprecision" and it was not possible to find any "Hasprecision" extension method that accepts a first argument of type ːPropertybuilder<double>' ". Would you know what it’s about?
– Victor Moraes
@Victormoraes Change your field to decimal type to use precision. It’s like double.
– George Wurthmann
I already changed there in the domain and keeps showing the error.
– Victor Moraes