lundi 7 décembre 2015

updating multiple records at once in linq

I want to update few records in a table at once.

So I have table like below

enter image description here

these are relevant model classes

AB_Product_vs_Field model class

public partial class AB_Product_vs_Field
{
    public string Product_ID { get; set; }
    public string Field_ID { get; set; }
    public string Field_Value_EN { get; set; }
    public string Field_Value_AR { get; set; }
}

ProductEdit model class

public class ProductEdit
{
    public string Product_ID { get; set; }
    public string ProductTypeID { get; set; }
    public string ProductCategoryID { get; set; }
    public string Subsidary_ID { get; set; }
    public string Field_ID { get; set; }
    public string ProductFieldNameEn { get; set; }
    public string ProductFieldNameAr { get; set; }
    public string ApprovalStatus { get; set; }
    public string Status { get; set; }
    public IList<AB_Product_vs_Field> ListProductFields { get; set; }
    public IList<AB_ProductTypeCategoryField> ListProductLables { get; set; }
}

Once I submit details I want to update all the relevant rows AB_Product_vs_Field table.

Eg: lets say I update Product_ID = 10 records , its should be update all the rows relavant to Product_ID = 10

So I created my linq query like this

    [HttpPost]
    [ValidateInput(false)]
    public ActionResult Product_Edit(ProductEdit product)
    {
        AB_Product_vs_Field insertproductvalue = new AB_Product_vs_Field();      
        var user = db.AspNetUsers.Find(User.Identity.GetUserId());


        if (ModelState.IsValid)
        {

            var listInsertproductvalue = new List<AB_Product_vs_Field>(); 

            for (int i = 0; i < product.ListProductFields.Count; i++)
            {
                listInsertproductvalue.Add(new AB_Product_vs_Field
                {
                    Product_ID = product.Product_ID,
                    Field_ID = product.ListProductFields[i].Field_ID,
                    Field_Value_EN = product.ListProductFields[i].Field_Value_EN,
                    Field_Value_AR = product.ListProductFields[i].Field_Value_AR
                }); 

            };

            db.Entry(listInsertproductvalue).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();

but here I'm getting following error in db.Entry(listInsertproductvalue).State = System.Data.Entity.EntityState.Modified; line

The entity type List`1 is not part of the model for the current context.

Aucun commentaire:

Enregistrer un commentaire