PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 23, 2022

[FIXED] How to update multiple rows of a database table in ASP.NET Core

 October 23, 2022     asp.net-core, entity-framework, entity-framework-core, sql-update     No comments   

Issue

I want to update multiple rows in ASP.NET Core, but I get an error:

InvalidOperationException: The entity type 'EntityQueryable' was not found. Ensure that the entity type has been added to the model.

This is my code:

var data = _db.UserTable.Where(a => a.CityIDn == selectedid);

foreach (var items in data)
{
    await Task.Run(() =>
                {
                    items.CityID = 2;
                });

    _db.Update(data);
    await _db.SaveChangesAsync();
}

Solution

Try this for multiple rows:

var data = _db.UserTable.Where(a => a.CityIDn == selectedid).ToList();

foreach (var item in data)
{
    item.CityID = 2;
    _db.UserTable.Update(item);
}
await _db.SaveChangesAsync();

And for one record, try like this:

var data = _db.UserTable.Where(a => a.CityIDn == selectedid).FirstOrDefault();
if(data != null)
{
    data.CityID = 2;
    _db.UserTable.Update(data );
    await _db.SaveChangesAsync();
}


Answered By - Hossein
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing