I need help in updating a row on a database table and stuck in that. Anybody please help me how to achieve this?
I want to check whether the product already exists on my database and if yes, i need to check whether any of the fields is been changed since its been inserted from JSon, if thats the case, then I need to update the fields.
My model looks like this
namespace MySite.Models
{
[TableName("Product")]
[PrimaryKey("ProductId", autoIncrement = false)]
[ExplicitColumns]
public class Product
{
[Column("ProductId")]
[PrimaryKeyColumn(AutoIncrement = false)]
public Guid ProductId{ get; set; }
[Column("ProductName")]
[NullSetting(NullSetting = NullSettings.Null)]
public string ProductName { get; set; }
[Column("ProductPrice")]
[NullSetting(NullSetting = NullSettings.Null)]
public string ProductPrice { get; set; }
[Column("Category")]
public string Category { get; set; }
}
}
Upon user request for a productid, i check the database for whether the product exists in case not exsits, I insert from a json string retrieved from external url:
var myProduct = this.DatabaseContext.Database.Fetch<Product>("Select * from Product where ProductId = @0", productId);
JSon looks like:
{"productId":"4632fdeb-0b8e-471f-a44a-0b07b5444656","ProductName":"MyProduct ABC 01","ProductPrice":"1000","Category":"1"}
My insert looks like this:
Product productInfo = null;
productInfo = JsonConvert.DeserializeObject<Product>(jsonResponse);
if (!myProduct.Any())
{
this.DatabaseContext.Database.Insert(productInfo);
}
I want to in the same way, update the product row from the same json string, in case it finds a record, how do I do that?
if (myProduct.Any())
{
//I want to update the matching product row on the database, how do I do that?
}