首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >System.Data.Entity.Infrastructure.DbUpdateConcurrencyException

System.Data.Entity.Infrastructure.DbUpdateConcurrencyException
EN

Stack Overflow用户
提问于 2021-06-02 12:47:14
回答 1查看 143关注 0票数 0

我得到了这个错误:

异常在这里的db.SaveChanges()行中发生;db.SaveChanges发生在EntityFramework.dll.Store update、insert或delete语句中,影响到意外的行数(0)。实体可能已被修改或删除,因为实体是加载的。

密码有什么问题吗?请帮帮我。下面是我的密码。

模型类:

代码语言:javascript
复制
public class EmployeeDb : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<Employee>().ToTable("EmployeeTable");
    }
    public DbSet<Employee> Employees { get; set; }
}

public class Employee
{
    [Key]
    public int EmployeeID { get; set; }
    public string EmployeeName { get; set; }
    public string EmployeeAddress { get; set; }
    public string EmployeeEmail { get; set; }
}

控制器类:

代码语言:javascript
复制
public class EmployeeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Create(Employee emp)
    {
        EmployeeDb dbobj = new EmployeeDb();
        dbobj.Employees.Add(emp);
        dbobj.SaveChanges();
        return View("Employee",emp);
    }
}

意见:

代码语言:javascript
复制
@model EmployeeMgmt.Models.Employee

@using (Html.BeginForm("Create","Employee",FormMethod.Post)) 
{
    @Html.AntiForgeryToken()   

<div class="form-horizontal">
    <h4>Employee</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.EmployeeID, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.EmployeeID, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.EmployeeID, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.EmployeeName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.EmployeeName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.EmployeeName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.EmployeeAddress, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.EmployeeAddress, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.EmployeeAddress, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.EmployeeEmail, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.EmployeeEmail, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.EmployeeEmail, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

是什么导致了这个错误,以及如何修复它?

EN

回答 1

Stack Overflow用户

发布于 2021-06-03 07:59:51

@Sushma您的Id字段必须包含唯一的编号。这通常是通过允许数据库为您选择该号码来完成的。在某些情况下,它可以由代码生成,但这在多用户系统中是很困难的。最简单的修复方法是将[DatabaseGenerated(DatabaseGeneratedOption.Identity)]添加到模型中的id字段。您还需要创建一个迁移来用这个新设置更新数据库。

代码语言:javascript
复制
public class Employee
{
  [Key]
  [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  public int EmployeeID { get; set; }
  public string EmployeeName { get; set; }
  public string EmployeeAddress { get; set; }
  public string EmployeeEmail { get; set; }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67805385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档