How can I make a C # decimal equal to a decimal SQL value to track EF changes?

To avoid touching immutable records in EF, it is important that the original and current values ​​of the entity match. I see a problem with decimals when an EF object has an SQL representation and which compares with C # decimal.

This is the debug output from objects with detected changes. This shows the problem quite clearly. Although both the object and the source data are of the decimal type, the values ​​are considered the difference, even if they are equal.

enter image description here

How can I guarantee that the original and current values ​​are consistent when using C # decimal?

Maybe there is a way to turn C # decimal into an entity (SQL) before the decimal point before updating?

Another example

enter image description here

I expect truncation to ignore the fact that incoming precision is above the SQL scale

+5
source share
2 answers

You can implement a proxy property that handles the conversion accurate to db precision:

public class MoneyClass { [Column("Money")] public decimal MyDbValue { get; set; } // You existing db-property [NotMapped] public decimal MyCodeValue // some property to access within you code { get { return this.MyDbValue; } set { decimal newDbValue = decimal.Round(value, 2); if (this.MyDbValue != newDbValue) { Console.WriteLine("Change! Old: {0}, New: {1}, Input: {2}", this.MyDbValue, newDbValue, value); this.MyDbValue = newDbValue; } } } } static void Main(params string[] args) { MoneyClass dbObj = new MoneyClass() { MyCodeValue = 123.456M }; Console.WriteLine(dbObj.MyDbValue); dbObj.MyCodeValue = 123.457M; // won't change anything Console.WriteLine(dbObj.MyDbValue); dbObj.MyCodeValue = 123.454M; // will change because of 3rd decimal value 4 Console.WriteLine(dbObj.MyDbValue); dbObj.MyCodeValue = 123.46M; // will change Console.WriteLine(dbObj.MyDbValue); } 
+3
source

This answer is not intended to correct exactly what you have, but to get around it.

I propose to encode logic that decides whether to save objects or not at a higher level of the application (in this regard, I consider the EF classes created as low-level objects).

The code that retrieves and stores the data can be implemented in the repository class, that is, in the class that controls your data access logic. So what you use will be this repository class, not EF code. Whether the repository class is internal using EF or something else will not be more important for your application. If you define an interface for your repository class, you can easily replace it with some or technologies for saving and retrieving data.

See here for a Microsoft article on the repository template. This is information from a question here in stackoverflow.

Normally, I would not recommend using classes generated by EF in normal application code. This may be tempting first, but also cause problems later, as in your case.

+2
source

Source: https://habr.com/ru/post/1275351/


All Articles