[英]Asp.net Core Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException:
i am creating simple crud system in asp.net core mvc with angular i ran into the problem with while updating the records record not updatedd.i got the error as我正在使用 angular 在 asp.net 核心 mvc 中创建简单的 CRUD 系统 我在更新记录时遇到了问题 record not updatedd. 我得到的错误是
Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException:
The database operation was expected to affect 1 row(s), but actually affected 0 row(s);
i could save and delete and view the records only i had a problwm with update records.please solve the problem.我只能保存、删除和查看记录我有更新记录的问题。请解决问题。
StudentController.cs学生控制器.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ReactAspCrud.Models;
namespace ReactAspCrud.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StudentController : ControllerBase
{
private readonly StudentDbContext _studentDbContext;
public StudentController(StudentDbContext studentDbContext)
{
_studentDbContext = studentDbContext;
}
[HttpGet]
[Route("GetStudent")]
public async Task<IEnumerable<Student>> GetStudents()
{
return await _studentDbContext.Student.ToListAsync();
}
[HttpPost]
[Route("AddStudent")]
public async Task<Student> AddStudent(Student objStudent)
{
_studentDbContext.Student.Add(objStudent);
await _studentDbContext.SaveChangesAsync();
return objStudent;
}
[HttpPatch]
[Route("UpdateStudent/{id}")]
public async Task<Student> UpdateStudent(Student objStudent)
{
_studentDbContext.Entry(objStudent).CurrentValues.SetValues(objStudent);
_studentDbContext.Entry(objStudent).State= EntityState.Modified;
await _studentDbContext.SaveChangesAsync();
return objStudent;
}
[HttpDelete]
[Route("DeleteStudent/{id}")]
public bool DeleteStudent(int id)
{
bool a = false;
var student = _studentDbContext.Student.Find(id);
if (student != null)
{
a = true;
_studentDbContext.Entry(student).State= EntityState.Deleted;
_studentDbContext.SaveChanges();
}
else
{
a = false;
}
return a;
}
}
}
Studentcrud.components.ts Studentcrud.组件.ts
setUpdate(data: any)
{
this.stname = data.stname;
this.course = data.course;
this.currentStudentID = data.id;
}
UpdateRecords()
{
let bodyData =
{
"stname" : this.stname,
"course" : this.course,
};
this.http.patch("https://localhost:7195/api/Student/UpdateStudent"+ "/"+ this.currentStudentID,bodyData).subscribe((resultData: any)=>
{
console.log(resultData);
alert("Student Registered Updateddd")
this.getAllStudent();
});
}
I think the problem might be that the DBContext does not know which properties changed exactly.我认为问题可能是 DBContext 不知道哪些属性发生了确切的变化。
Use the Find()
method to get the entity, then you can使用
Find()
方法获取实体,然后就可以
_studentDbContext.Entry(existingObject)..CurrentValues.SetValues(objStudent);
Not sure if you can do the same using the Attach
method too to save a call to the DB, and (if it works) update all fields whether changed or not.不确定您是否也可以使用
Attach
方法执行相同的操作以保存对数据库的调用,并且(如果有效)更新所有字段,无论是否更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.