0

here is my code block

 foreach(var n in animalbirdAdoptionDetails)
 {
    int animalBirdsId = n.AnimalsAndBirdsId;
    int NoAnimal =  Convert.ToInt32(n.NoAnimalsAdopted);
    n.isActive = false;
    context.NameOfAnimalsAndBirds.Update(e2 => new entity 
                     { 
                         quantity = e2.quantity + moreQuantity
                     });
    context.SaveChanges();

  }

DbSet' does not contain a definition for 'Update' and no accessible extension method 'Update' accepting a first argument of type 'DbSet' could be found (are you missing a using directive or an assembly reference?)

Hardik
  • 3,038
  • 2
  • 19
  • 31
shivu
  • 153
  • 3
  • 14
  • **1st** Please make sure that you have the `System.Linq;` namespace included. **2nd** are you trying to update multiple `records/entities` ? – vikscool May 28 '19 at 05:00
  • @vikscool i have installed this package [link](https://www.nuget.org/packages/Z.EntityFramework.Plus.EF6/) according to this stack-overflow solution [link](https://stackoverflow.com/questions/34135748/entity-framework-update-add-value-to-existing-value) after that i'm getting this error `The type or namespace name 'entity' could not be found (are you missing a using directive or an assembly reference?) ` – shivu May 28 '19 at 05:05
  • that's ok but do you have a namespace in your `.cs` page stated as `Using System.linq;`? – vikscool May 28 '19 at 05:07
  • @vikscool yes i do have. and I'm trying to add the values with existing column value – shivu May 28 '19 at 05:08
  • I think you are using the extended form of `EntityFramework`(*i.e. EntityFramework.Plus*) and as per their documentation in order to use the [Batch Update](https://entityframework-plus.net/batch-update) you have to add a filter expression(*i.e. Where() clause*). And also are you sure you want to use [EntityFramework.Plus](https://www.nuget.org/packages/Z.EntityFramework.Plus.EF6/) and not [EntityFramework](https://www.nuget.org/packages/EntityFramework/6.3.0-preview5-19254-05)? – vikscool May 28 '19 at 05:13
  • are you trying to update all the values of a column or just a single column of a single row? Because of the query(`context.NameOfAnimalsAndBirds.Update(e2 => new entity { quantity = e2.quantity + moreQuantity }); `) that you have written will be translated in SQL as `Update NameOfAnimalsAndBirds set quantity=quantity+moreQuantity;` which is without any `where` clause and will update all the values of `quantity` column in the table `NameOfAnimalsAndBirds`. is that what you are looking for? – vikscool May 28 '19 at 05:27
  • follow this [link](https://stackoverflow.com/a/25898203/6797509) – Hamed Moghadasi May 28 '19 at 05:54

2 Answers2

3

You are facing this error because in entity framework there is no update extension method available.

If you want to update existing records then you need to retrieve existing record by primary key reference or by any other table field reference. and assign new value to table fields.

Eg.

foreach(var n in animalbirdAdoptionDetails)
{
    // remove unwanted variables and casting expression.
    // if not required then.
    int animalBirdsId = n.AnimalsAndBirdsId;
    int NoAnimal =  Convert.ToInt32(n.NoAnimalsAdopted);
    n.isActive = false;

    // Retrieve existing record
    var entity = context.NameOfAnimalsAndBirds.FirstOrDefault(x => x.Id == n.Id);

    // assign new value to existing property.
    entity.quantity = entity.quantity + moreQuantity;

    // in last just apply save changes.
    context.SaveChanges();
}

Note: above code is for demonstration only. it is always good practice to update all entities at once and apply save changes (from outside of loop).

Hardik
  • 3,038
  • 2
  • 19
  • 31
  • thanks,I need to update **only one** column with existing value.am i use the above code(your code) to update? – shivu May 28 '19 at 05:20
  • Yes, you can do like this also – Hardik May 28 '19 at 05:23
  • You can also use AddOrUpdate (you need to add an extra reference for this). I use this all the time with projects that use a different front-end tech (e.g. angular, react) and talk with a .net api, in this way you can just send the entire object and just use 1 single method, instead of having to map fields from one object to another or set a field manually. But be careful because if it doesn't recognize the object to be updated it will add it instead. – WtFudgE Dec 16 '19 at 13:52
  • I think for better performance, you'd want the `context.SaveChanges` outside the foreach loop? – Sum None Feb 19 '20 at 10:35
0
foreach(var n in animalbirdAdoptionDetails)
{
   int animalBirdsId = n.AnimalsAndBirdsId;
   int NoAnimal =  Convert.ToInt32( n.NoAnimalsAdopted);
   n.isActive = false;

   // Retrieve existing record into your model class
   NameOfAnimalsAndBird model = context.NameOfAnimalsAndBirds.Find(n.Id);

    model.quantity = model.quantity + moreQuantity;  
    context.SaveChanges();
}
safeena rasak
  • 390
  • 2
  • 5
  • 16