I am using ASP.NET MVC and I am also using MS SQL server.
Lets say I have a class/model like this:
public class Product{
public int ID { get; set; }
public string WH {get; set;}
public decimal WEIGHT { get; set; }
public System.DateTime MAINT_DATE {get; set;}
}
So there is a View to capture any updates for a product. Right now, when I click save button on the View, the maintenance date or MAINT_DATE for a product is updated in the database.
However, lets say my requirements is a little bit different. When I press save button in the View, I need to check if there is any changes in the product, if there is no changes in properties for WH
and WEIGHT
, even if I press save button, the maintenance date should not be updated.
If I were to do this when using Entity Framework, I would be of course using something like the one below:
Product currentProduct = db.Products.Find(productID);
var oldWHvalue = currentProduct.WH;
var oldWEIGHTvalue = currentProduct.WEIGHT;
So I will just go to database and check the 'previous' value that is stored in the database and compare it with the new values that the user submit from the form. This seems ok for the above scenario. However, if the Model has alot of properties, maybe 30 or more properties, that would mean I have to write alot of lines of code similar to the one above, to check if there is any change for any property of the object.
My question is whether there is some other method or technique that I am not aware of. So maybe originally the record for Product
ID 1 in the database is
WH => "Warehouse 2"
WEIGHT => 33.50
then after an update, the record for Product
ID 1 in the database becomes:
WH => "Warehouse 1"
WEIGHT => 30
Is there some sort of technique where I can get the 'image' of the database record of this Product ID 1 before and after the update and compare the values?