Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
6.3k views
in Technique[技术] by (71.8m points)

asp.net - How to implement post method and auto increment id[pk] in web api?

When I am passing the values using post method in postman, Id is passing as 0 everytime, where id is the primary key in my database table. 
    
Here is my model :
       public class WoodlandDataModel
        {
            public int id { get; set; }
            public string cph_id { get; set; }
            public int subactivity_id { get; set; }
        }
    
    And Given below is my controller: 
    
    public IHttpActionResult Post(WoodlandDataModel data)
             {
                 if (!ModelState.IsValid)
                     return BadRequest("Invalid data.");
    
                 using (var entity = new WoodlandDB_Entities())
                 {
                     entity.tbl_woodland_details.Add(new tbl_woodland_details()
                     {
                         cph_id = data.cph_id,
                         subactivity_id = data.subactivity_id
                     });
    
                     entity.SaveChanges();
                 }
    
                 return Ok();
             }
    

I want that when I will pass any value, id will be auto increment in the database.

Can somebody help me to find out the solution.

Any help will be highly appreciated!

Rutuparna Panda


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...