我得到了这个错误:
异常在这里的db.SaveChanges()行中发生;db.SaveChanges发生在EntityFramework.dll.Store update、insert或delete语句中,影响到意外的行数(0)。实体可能已被修改或删除,因为实体是加载的。
密码有什么问题吗?请帮帮我。下面是我的密码。
模型类:
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; }
}控制器类:
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);
}
}意见:
@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>是什么导致了这个错误,以及如何修复它?
发布于 2021-06-03 07:59:51
@Sushma您的Id字段必须包含唯一的编号。这通常是通过允许数据库为您选择该号码来完成的。在某些情况下,它可以由代码生成,但这在多用户系统中是很困难的。最简单的修复方法是将[DatabaseGenerated(DatabaseGeneratedOption.Identity)]添加到模型中的id字段。您还需要创建一个迁移来用这个新设置更新数据库。
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; }
}https://stackoverflow.com/questions/67805385
复制相似问题