INSTEAD OF INSERT 设置的主键在 entity framework 中抛出错误(ASP.NET 框架 c#)

primary key set by INSTEAD OF INSERT throwing error in entity framework (ASP.NET framework c#)

我刚开始使用 .NET,一直在尝试对 table 执行 CRUD 操作。 table 有一个设置主键的触发器。现在,读取、更新和删除操作可以顺利进行。我的问题是创建操作。

这是触发器

create or alter trigger dbo.CreateEmpID on dbo.Employee instead of insert
as
begin

declare @NextId varchar(20)
declare @EmpName varchar(50)
declare @cur cursor
set @cur = cursor for select EmpName from inserted
open @cur
fetch next from @cur into @EmpName
while @@FETCH_STATUS = 0
begin
    select @NextId = 'EMP' + convert(varchar(20), isNull(max(convert(int, substring(EmpID, 4, len(EmpID)-3))),0)+1) from Employee
    insert into Employee(EmpID, EmpName) values (@NextId, @EmpName)
    fetch next from @cur into @EmpName
end
close @cur
deallocate @cur

end

这是创建新员工的代码部分

//POST: Create
    [HttpPost]
    public ActionResult Create(Employee Emp)
    {
        DB.Employees.Add(Emp);
        DB.SaveChanges();
        return RedirectToAction("Index");
    }

这是我不断收到的错误

System.Data.Entity.Validation.DbEntityValidationException: 'Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.'

我还对另一件事感到困惑。同一个 table 上还有另一个触发器在 DELETE 后起作用,它将删除的员工复制到另一个 table。那么是 INSTEAD OF 触发器有问题还是代码有问题。

感谢任何帮助。

编辑:

我更改了 table 以使用第一个答案中提到的序列,但它仍然给我错误。这也是详细的错误

System.Data.Entity.Validation.DbEntityValidationException
  HResult=0x80131920
  Message=Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
  Source=EntityFramework
  StackTrace:
   at System.Data.Entity.Internal.InternalContext.SaveChanges()
   at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
   at System.Data.Entity.DbContext.SaveChanges()
   at Test.Controllers.HomeController.Index() in E:\vs\projects\Test\Test\Controllers\HomeController.cs:line 18
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c.<BeginInvokeSynchronousActionMethod>b__9_0(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_0.<InvokeActionMethodFilterAsynchronouslyRecursive>b__0()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()

此处可能会出现许多问题,但不值得进行故障排除,因为您永远不应该通过从目标 table 查询 max(id)+1 来生成密钥。它无法扩展并且容易产生死锁。

如果您想要像 EMP1234 这样的密钥,您可以使用序列和默认值生成它,请参见示例和

但通常情况下,您只需使用序列或身份列并继续。