Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

EF Core with an Existing Database Puy method does not work!

$
0
0

Hi All,

I've applied this topic at this link:

https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db

but for web api project and when I try to apply put method by using fiddler as this:

I'm using this code in put method which generated by "API Controller with views, using Entity Framework" : (with some update by me)

    [HttpPut("{id}")]
        public async Task<IActionResult> PutEmployees([FromRoute] int id, [FromBody] Employees employees)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest("Model Error");
                //return BadRequest(ModelState);
            }

            if (id != employees.ID)
            {
                return BadRequest("Employee with id = " + id.ToString() + " not found to update");
            }

            _context.Entry(employees).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EmployeesExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

The fiddler returns me error: Bad Request with the message: Employee with id = 5  not found to update!

Why? and how to solve?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>