简体   繁体   English

Asp.net 核心 Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException:

[英]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.

相关问题 Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException - Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException OracleModificationCommandBatch.Consume():Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException - OracleModificationCommandBatch.Consume() : Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException 抛出异常:System.Private.CoreLib.ni.dll中的&#39;Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException&#39; - Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException' in System.Private.CoreLib.ni.dll 实体框架:更新具有 IEnumerable 属性的实体时出错。 &#39;Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException&#39; - Entity Framework: Error updating an Entity with IEnumerable property. 'Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException' Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException:'数据库操作预计会影响 1 行,但实际上会影响 2 行 - Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: 'Database operation expected to affect 1 row(s) but actually affected 2 row(s) 带有 EntityFrameworkCore 的 ASP.NET Core 中的 SQLite - SQLite in ASP.NET Core with EntityFrameworkCore ASP.NET Core 2 无法解析 Microsoft EntityFrameworkCore DbContext 类型的服务 - ASP.NET Core 2 Unable to resolve service for type Microsoft EntityFrameworkCore DbContext 升级到 ASP.NET Core 5.0 时 Microsoft.EntityFrameworkCore.Relational 的版本冲突 - Version conflict for Microsoft.EntityFrameworkCore.Relational when upgrading to ASP.NET Core 5.0 无法在 ASP.NET Core 中安装“Microsoft.EntityFrameworkCore.Tools.Dotnet” - Can't install "Microsoft.EntityFrameworkCore.Tools.Dotnet" in ASP.NET Core 在ASP.Net Core 1.0中注册通用EntityFrameworkCore DbContext - Register generic EntityFrameworkCore DbContext in ASP.Net Core 1.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM