avatarJen-Hsuan Hsieh (Sean)

Summary

The article discusses how to set precision for Decimal type in Entity Framework Code First for SQL Server.

Abstract

The article explains that SQL Server requires a decision on the precision for the decimal type, such as decimal(10, 3) for 7 integer places and 3 decimal places. However, when using Entity Framework Code First, the precision for the C# code mapping to SQL Server is fixed to decimal(18, 2). The article provides two methods to solve this problem: setting precision by using modelBuilder or writing a customized convention. The first method involves modifying the OnModelCreating() method, while the second method involves creating a customized attribute and convention.

Opinions

  • The author suggests that the solution to the problem is quite simple.
  • The author provides two methods to solve the problem, indicating that there may be multiple solutions.
  • The author acknowledges the disadvantage of the first method, which is that the developer has to remember to modify the OnModelCreating() method.
  • The author provides an alternative way to solve the problem by creating a customized convention.
  • The author provides code examples for both methods, indicating that the solution is practical and implementable.

ASP .NET MVC — Set precision for Decimal for Entity Framework Code First

We found that the table only contained two decimal places and we want to save more place in the SQL server.

Introduction

For SQL Server, we have to decide the precision for the decimal type. For example, decimal(10, 3) means 7 integer place and 3 decimal place.

However, the precision for the C# code mapping to SQL Server is fixed to decimal(18, 2) when we use Entity Framework Code First.

The solution is quite simple.

Solution

There are 2 methods to solve this problem.

1. Set precision by using modelBuilder

The first method is to set precision by using modelBuilder.

2. Write customized convention

The disadvantage of the first method to solve this problem is that you have to remember to modify OnModelCreating(). An alternative way is to create a customized convention.

Copy right@A layman
  1. Create a convention which inherits from the PrimitivePropertyAttributeConfigurationConvention. It can help us to configure the property from ConventionPrimitivePropertyConfiguration.

2. Create a customized attribute which inherits attribute. It’s the type to enter the PrimitivePropertyAttributeConfigurationConvention.

3. Tell Entity Framework to use our convention. Add it to conventions collection in OnModelCreating.

4. Decorate our model by using this convention.

[DecimalPrecision(10, 3)]
public decimal Longitude { get; set; }

References

Summary

Thanks for your patient. I am Sean. I work as a software engineer.

This article is my note. Please feel free to give me advice if any mistakes. I am looking forward to your feedback.

Please feel free to clap if this article can help you. Thank you.

Other topics

You can also subscribe my page on Facebook.

Related topics

How to use the two-way binding in Knout.js and ReactJS?

Learn how to use SignalR to build a chatroom application

My reflection of :

APM & Logging Services:

IT & Network:

Database:

Software testing:

Debugging:

DevOps:

Programming
Software Development
Database
Debugging
Aspnet
Recommended from ReadMedium