Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why does Entity Framework not generate and auto increment the Id's?
C#
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace TwoFactorAuthentication.API.Models
{
    public class leave
    {
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity), Key()]
        public int Id { get; set; }
        public DateTime date { get; set; }
        public decimal length_hours { get; set; }
        public decimal lenght_days { get; set; }
        public int status { get; set; }
        public string comments { get; set; }
        public int leave_request_id { get; set; }
        [Required(ErrorMessage = "Leave Type ID")]
        public int leave_type_id { get; set; }
        public int emp_number { get; set; }
        [Required(ErrorMessage = "Start Time")]
        public DateTime start_time { get; set; }
        [Required(ErrorMessage = "End Time")]
        public DateTime end_time { get; set; }
        public int duration_type { get; set; }
    }
    public class leave_request
    {
        
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity), Key()]
        public int id { get; set; }
        public DateTime date_applied { get; set; }
        public int leave_type_id { get; set; }
        public int emp_number { get; set; }
        public string comments { get; set; }


    }
}

my sql table
SQL
SELECT * FROM hrmssoft.aspnetusers;
CREATE TABLE leave_request` (
  `id` int(10) unsigned NOT NULL,
  `leave_type_id` int(10) unsigned NOT NULL,
  `date_applied` date NOT NULL,
  `emp_number` int(7) NOT NULL,
  `comments` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `emp_number` (`emp_number`),
  KEY `leave_type_id` (`leave_type_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

controller class-
C#
public async Task<ihttpactionresult> CreateLeaveRequest(ohrm_leave_request leaverequest)
        {                   
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }           
            leaverequest.date_applied = DateTime.Now;
           lvcontext.ohrm_leave_request.Add(leaverequest);           
            await lvcontext.SaveChangesAsync();
            return Ok();            
        }


What I have tried:

My sql data base table

id` int(10) unsigned Primary NOT NULL,

for id auto-generate.so i am use
this code
C#
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity), Key()]
        public int id { get; set; }

i send these value through postman
HTML
{  	"leave_type_id":"4",
     "emp_number":87,
     "comments":"Hello how are you !."
}
Posted
Updated 26-Jun-19 8:42am
v3
Comments
F-ES Sitecore 25-Jun-19 9:24am    
The id field needs to be an IDENTITY field in your database.
TCA1105064 25-Jun-19 9:40am    
but i don't want set id identity.
it is not possible to without "identity" .
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity), Key()]
public int id { get; set; }
F-ES Sitecore 25-Jun-19 9:47am    
If you don't want to use an IDENTITY then you can't have sequential IDs. You could manually increment the IDs yourself but that will fail at some point due to concurrent requests both thinking their ID+1 is the "next" ID. Leaving the database to work this stuff out using IDENTITY solves that problem.
Maciej Los 26-Jun-19 14:47pm    
What's database provider?

1 solution

That's because Entity Framework never generates ID values. EF has no facility to generate ID values at all.

What you should be doing is telling EF is that the database will generate the ID values and let the database generate them.
 
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