Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Problem

How 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 below


C#
public 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 that
How 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

    }
Posted
Updated 16-Feb-19 2:07am

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[^]
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 16-Feb-19 6:15am    
This will give him some unusual jumps in the ID values, if he has not used it properly, or would require him to properly sanitize the data too.

https://stackoverflow.com/questions/14162648/sql-server-2012-column-identity-increment-jumping-from-6-to-1000-on-7th-entry
Richard Deeming 18-Feb-19 10:56am    
One of those lovely bugs where all of the workarounds documented on Microsoft's "Connect" feedback site were thrown away when they decided to retire it. 🤦‍♂️

Thankfully, it looks like Mithandrir documented the main workarounds in that thread, rather than relying on a link to the Connect bug report.
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,
C#
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.
 
Share this answer
 
Comments
Richard Deeming 18-Feb-19 10:58am    
Why string and not Guid?
Afzaal Ahmad Zeeshan 18-Feb-19 14:56pm    
To be honest, I have not yet tried that, the idea is to use the UUID type to store the data, and since EF has to map the column to UUID, string can be useful—I will have to try it with Guid, and that too with different data sources, not just SQL Server.

Thanks for the hint, though. :-)

Also, recently I started using Sequelize ORM, and that has a datatype for that, which I have been using. I will consider using this sometime.
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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900