Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

WebApi2 POST. Controller return CreatedAtRoute but no record inserted.

$
0
0

Subject pretty clear explaining my problem. When I'm trying to insert new record using POST action of WebApi Controller, it returning CreatedAtRoute object with Id that always null. Controller code are pretty simple and I really do not have idea what is wrong (especially when similar Controller working just fine).

[ResponseType(typeof(Sets))]
        public IHttpActionResult PostSets(Sets sets)
        {

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            try
            {
                db.Sets.Add(sets);
                db.SaveChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                    }
                }
            }
            return CreatedAtRoute("PreppApi", new { id = sets.Id }, sets);
        }

CreatedAtRoute returning correct data, that was passed to, but Id are always null and record not inserted.

Here is how model looks:

namespace PREPP2.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Sets
    {
        public int Id { get; set; }
        public string SetID { get; set; }
        public string ItemName { get; set; }
        public string ItemDescription { get; set; }
        public string ItemUrl { get; set; }
        public string ItemImageUrl { get; set; }
        public decimal ItemPrice { get; set; }
    }
}

And database table definition:

CREATE TABLE [dbo].[Sets] (
    [Id]              INT            IDENTITY (1, 1) NOT NULL,
    [SetID]           NCHAR (36)     NOT NULL,
    [ItemName]        NVARCHAR (50)  NOT NULL,
    [ItemDescription] NVARCHAR (250) NOT NULL,
    [ItemUrl]         NVARCHAR (250) NOT NULL,
    [ItemImageUrl]    NVARCHAR (500) NOT NULL,
    [ItemPrice]       SMALLMONEY     NOT NULL,
    CONSTRAINT [PK_Sets] PRIMARY KEY CLUSTERED ([Id] ASC)
);

 


Viewing all articles
Browse latest Browse all 4850

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>