Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I've simple table Test which has testId(primary key, indentity auto increament, not null) and testName. I'm using Web api2 and entity framework to get and post data. I need to post only testName testId should be set automotically. How it will be correct?
C#
// POST: api/Checks
        [ResponseType(typeof(Check))]
        public IHttpActionResult PostCheck(Test test)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Tests.Add(test);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (TestExists(test.testId))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = test.testId }, test);
        }
Posted
Comments
VishwaKL 5-Jan-16 0:21am    
I think you have to make some setting like "SET IDENTITY_INSERT Table_Name ON". Or you should send only other details apart of ID which is primary key

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