The problem that the decimal type of linq is saved to the database is saved to only two digits after the decimal point, linqdecimal

Source: Internet
Author: User

The problem that the decimal type of linq is saved to the database is saved to only two digits after the decimal point, linqdecimal

The problem of saving a decimal type to data today has plagued me for a long time, finally, a small setup problem is solved ····

If you don't talk much about it, just ask the question and say the answer:

Problem: when using EF DbContext To Save decimal type data to the database, by default, only the first two decimal places are saved, and the remaining decimal places are set to 0;

For example, if 1.23456789 is saved, the actual data saved to the database is 1.23000000;

It's just a pitfall. If it's accurate to multiple digits after the decimal point, you can only give it to two of you. This is definitely not enough for me. What else can be set as powerful as linq, this problem is solved as follows:

Solution: when creating a DbContext, override the DbContext. OnModelCreating () method, and then specify the accuracy using the following method:

Protected override void OnModelCreating (DbModelBuilder modelBuilder) {// solve the problem of saving decimal data to the database in DbContext. By default, only the first two decimal places after the decimal point are saved, and the remaining places are set to 0, in this way, the modelBuilder with the decimal point is saved. entity <Models_VideoDeviceInfo> (). property (p => p. longitudeX ). hasPrecision (18, 7); modelBuilder. entity <Models_VideoDeviceInfo> (). property (p => p. latitudeY ). hasPrecision (18, 7); modelBuilder. entity <Models_VideoResource> (). property (p => p. latitude ). hasPrecision (18, 7); modelBuilder. entity <Models_VideoResource> (). property (p => p. longpolling ). hasPrecision (18, 7 );}

It's just that simple. It takes me half a day to complete my work ······

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.