Entity Framework 7 Set Decimal Accuracy for Model Builder

I tried to figure out how to set decimal precision for EF7 (Beta 4) without any luck.

I expected to do something like:

modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).Precision(10, 6) 

This doesn't seem to be available, but I was able to find the following class in the repository on GitHub:

https://github.com/aspnet/EntityFramework/blob/7.0.0-beta4/src/EntityFramework.Relational/RelationalDecimalTypeMapping.cs

There are no examples of using RelationTypeMapping classes or method signatures with them. Maybe this is just used as part of the api mapping to retrieve information?

Another place I can expect is this:

 modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).ForRelational().ColumnType() 

or

 modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).ForSqlServer().ColumnType() 

It only requires a string, is this functionality simply not implemented or am I just not looking in the right place?

Edit: I just realized that the string is probably suitable for the type .ColumnType ("decimal (10.6)") until it is built further, but still not averse to getting some clarification, although since I would chose not to use strings for this

Edit: after clarifying from bricelam, I ended up creating the following extension, which needs to be used to avoid using a string, and I appreciate the simplicity of their approach:

 public static RelationalPropertyBuilder DecimalPrecision(this RelationalPropertyBuilder propertyBuilder, int precision, int scale) { return propertyBuilder.ColumnType($"decimal({precision},{scale})"); } 

Usage example:

 modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).ForRelational().DecimalPrecision(10,6); 

Edit: Making changes to RC1

I have not tested them yet, but I just put together the following 2 examples of how this will look like RC1

  public static PropertyBuilder DecimalPrecision(this PropertyBuilder propertyBuilder, string precision, string scale) { return propertyBuilder.HasColumnType($"decimal({precision},{scale})"); } public static PropertyBuilder SqlDecimalPrecision(this PropertyBuilder propertyBuilder, string precision, string scale) { return propertyBuilder.ForSqlServerHasColumnType($"decimal({precision},{scale})"); } 

Since I have not tried this yet, I am not sure what will be the correct use between "HasColumnType" or "ForSqlServerHasColumnType", but hopefully this will point someone in the right direction.

+7
entity-framework-core
source share
2 answers

Your workaround is the design we intended. Instead of having a bunch of "faces", you can set a type such as precision, scale, maximum length, unicode / ansi, fixed / variable length, etc. We decided to keep it simple: if default type matching is not what you want, tell us which type to use. There was talk of reverting to this decision and reintroducing the “aspects”. If you are determined to do so, I recommend that you create a new problem .

Also note that at the moment there are several other type-matching errors, but they should be fixed by the time beta5 is released.

+4
source share

The example shown looks deprecated according to EF RC1.

This is how I set the precision in the decimal field.

Say I have an entity

 public class Review { public int ReviewId { get; set; } public decimal TotalScore { get; set; } //I want a precision field in DB public DateTime CreatedOn { get; set; } [Timestamp] public byte[] RowVersion { get; set; } } 

then in my class context, when creating a model, I create a mapping (I could do the mapping there, but I like to separate it)

 public class MyDbContext : DbContext { public MyDbContext(DbContextOptions<MyDbContext> options ) : base(options) { } public DbSet<Review> Reviews { get; set; } //etc. protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); //Mappings new ReviewMap(modelBuilder.Entity<Review>()); //etc.. } } 

and then display. Remember to use the namespace in which the model extensions are located:

 using Microsoft.Data.Entity; //here is where the extensions are public class ReviewMap { public ReviewMap(EntityTypeBuilder<Review> entityBuilder) { entityBuilder.HasKey(r => r.ReviewId); //Using the column type extension entityBuilder.Property(r => r.TotalScore) .HasColumnType($"decimal(5,2)") .IsRequired(true); //and this has nothing to do with the example but it interesting //to show how to use Sql command to automatically fulfil a value //when adding a new Entity entityBuilder.Property(r => r.CreatedOn) .ValueGeneratedOnAdd() .HasDefaultValueSql("GETUTCDATE()") .IsRequired(true); } } 
+1
source share

All Articles