[英]SqlException: Cannot insert explicit value for identity column in table 'Ogretmenler' when IDENTITY_INSERT is set to OFF
i am getting this error SqlException: Cannot insert explicit value for identity column in table 'Ogretmenler' when IDENTITY_INSERT is set to OFF.我收到此错误 SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表 'Ogretmenler' 中插入标识列的显式值。 Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, bool breakConnection, Action wrapCloseInAction) Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException 异常,bool breakConnection,Action wrapCloseInAction)
DbUpdateException: An error occurred while saving the entity changes. DbUpdateException:保存实体更改时出错。 See the inner exception for details.有关详细信息,请参阅内部异常。 Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection 连接
what should i do?我应该怎么办?
here is my code这是我的代码
[HttpGet] [HTTPGet]
public IActionResult Create()
{
List<SelectListItem> value = (from Ders in _db.Dersler.ToList()
select new SelectListItem
{
Text = Ders.DersName,
Value = Ders.Id.ToString()
}).ToList();
ViewBag.v1 = value;
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Ogretmen obj)
{
if (ModelState.IsValid)
{
_db.Ogretmenler.Add(obj);
_db.SaveChanges();
return RedirectToAction("Index");
}
return View(obj);
}
The exception 'Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF' is thrown when you attempt to insert a value in the Identity Column.当您尝试在标识列中插入值时,将引发异常“当 IDENTITY_INSERT 设置为 OFF 时无法在表中为标识列插入显式值”。
Either:任何一个:
or或者
I had this problem because key field for my table has attributes [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)]我遇到了这个问题,因为我的表的键字段具有属性 [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)]
Deleted Database generated and no problem anymore已删除生成的数据库,不再有问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.