本文介绍了在Web API项目上创建新员工时如何获得最后一条记录+ 1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 问题 如何通过网络API表示创建新的员工记录? $ b $项目asp.net核心的工作2.1 i创建mvc控制器员工控制器并在其中我创建创建动作结果新员工视图。 使用get类型创建函数并显示数据库中存在的最后一条记录以及下面的一条 public IActionResult Create() { var model = new Employee(); if (id == null ) { model.EmployeeId = _repository.GetAll()。Max(Employee = > Employee.EmployeeId)+ 1 ; } return 查看(模型); } 我需要将动作结果create转换为web API以连接到angular 以便 如何将函数操作结果create转换为web API? 以便我创建新的Web API控制器 我尝试过: [Produces(application / json)] [Route(api / Employee)] 公共类EmployeeController:Controller { //如何使用web api编写用于创建的代码 } 解决方案 不要这样做!永远不要尝试预先分配ID号码,当您有多个用户访问数据库时很难找到,这会导致生产中的细微和间歇性错误,这些错误很难追踪,甚至更难解开数据。 查看使用DB IDENTITY字段 - 数据库将为您管理ID,并在每次添加行时为您提供新值。 IDENTITY字段MVC - Google搜索 [ ^ ] 除了解决方案1所说的,我还需要推荐另一种方法。这种方法是,使用string作为Entity Framework Core中的ID字段,框架将自动使用Guid生成唯一ID。我一直用这个,这让我晚上安然入睡。 进行以下更改, public class 员工{ public string EmployeeId { get ; set ; } } 并将其他所有内容留给EF Core。 有什么缺点?好吧,您无法查看员工之前或之后是否通过查看他们的ID进行了注册。但是,您可以通过 joinedDate 字段来判断。此外,SQL Server特别使用种子值。这可能会呈指数级增长,并且会产生诸如1,2,3,10,15,17,18,100,101,500等答案。那样想吗?不完全是。因此,如果可以的话,请接受我的建议,使用字符串作为主要关键字段并安居乐业。 :-) 第7个条目的SQL Server 2012列标识增量从6跳到1000+ - 堆栈溢出 [ ^ ] 我可以假设你真的想在他们的ID或其他东西上展示这个ID,在这种情况下,尝试将EmployeeNumber存储为一个单独的字段存储序数值,在它们加入你的组织时进入序列,并将主键保留为ORM- 我对道规化 - 需要 - 极客的道歉所产生的那个。 Quote:如何在Web API项目上创建新员工时获取最后一条记录+ 1? 坏主意! 这种方法忘记了应用程序是联网的,这意味着您可以有2个或更多用户同时添加员工,这意味着他们将拥有相同的下一个员工ID。 唯一安全的方法是让服务器在创建记录时生成id。 SQL AUTO INCREMENT a Field [ ^ ProblemHow to represent create new employee record by web API ?i work on project asp.net core 2.1 i create mvc controller employee controller and inside it i make create action result for create new employee view .Create function with get type and show last record exist on database plus one as belowpublic IActionResult Create() { var model = new Employee(); if (id == null) { model.EmployeeId = _repository.GetAll().Max(Employee => Employee.EmployeeId) + 1; } return View(model); }I need to convert action result create to web API to connect to angular so thatHow to convert function action result create to web API ?so that i create new web API controller What I have tried:[Produces("application/json")] [Route("api/Employee")] public class EmployeeController : Controller {//how to write code for create by using web api } 解决方案 Do not do that! Never try to "Pre-allocate" ID numbers, that leads to subtle and intermittent bugs in production when you have multiple users accessing the DB which are very hard to track down, and even harder to untangle the data.Look at using a DB IDENTITY field instead - the DB will manage the ID for you, and give you a new value each time you add a row. IDENTITY field MVC - Google Search[^]Apart from what Solution 1 tells, I have to recommend another approach. That approach is, to use string as the ID field in Entity Framework Core, and framework will automatically use Guid to generate the unique IDs. I have always used this, and this lets me sleep peacefully at night. Make the following changes, public class Employee { public string EmployeeId { get; set; }}And leave everything else to the EF Core. What are the downsides? Well, you cannot see if an employee was registered before, or later just by looking at their ID. However, you can tell that by their joiningDate field. Also, SQL Server notably uses a seed value. That can grow exponentially and will result is answers like, 1, 2, 3, 10, 15, 17, 18, 100, 101, 500, and so on. Do that want that? Not exactly. Thus, take my advice if you can, use string as a primary key field and live at peace. :-) SQL Server 2012 column identity increment jumping from 6 to 1000+ on 7th entry - Stack Overflow[^]I can assume that you really want to showcase this ID on their ID or something too, in that case, try to store that EmployeeNumber as a separate field that stores the ordinal value, coming in the sequence as they join your organization, and leave the primary key to be the one generated by ORM—my apologies to the normalization-is-needed-geeks.Quote:How to get last record+1 when create new employee on web API project ?Bad idea !This approach forget that the app is networked,this mean that you can have 2 users or more that add employee at same time, this mean that they will all have the same 'next employee id'.The only safe approach is to have the server generating the id when record is created.SQL AUTO INCREMENT a Field[^] 这篇关于在Web API项目上创建新员工时如何获得最后一条记录+ 1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-31 09:02