Reputation: 11
I am trying to update my DB table and from my model I am getting the id
public void Update(Abc model)
{
//Database Table Instance
Abc a=new Abc();
//Trying to update the column where id =id
try
{
a.id = model.Asset.id;
a.column = "R";
db.Entry(a).State = EntityState.Modified;
db.SaveChanges();
}
catch (Exception e) { }
}
It set the column to R bt sets Nulls for all other columns because I am not getting values for other columns from model. One way is to set the hidden values in model and send the model but the table has about 30 columns. so I want to get the table row from database where id=id and the only updatte the column in that row in my function in repository .....
Upvotes: 1
Views: 2581
Reputation: 14282
In order to achieve this first you need to get the actual object from your db context and then change the desired property and save it to in database like below:
try
{
Abc a = db.Abcs.SingleOrDefault(a => a.id == model.Asset.id);
a.column = "R";
db.SaveChanges();
}
catch (Exception e) { }
This will fix your concern.
Upvotes: 1