c# - Web API error: "Cannot insert the value NULL into column 'Id' -


i using web api , using methods in fiddler sample data in table, can data it.

when try insert new record using post method, error:

"cannot insert value null column 'id', table 'members'; column not allow nulls. insert fails. statement has been terminated."

the thing is, supplying value id in json string. copying json string get/id method , changing id.

i have column on table called id set not allow nulls, , not set auto increment. because requirements of system require present , unique. doesn't have sequential. want person inserting able supply own id.

i don't understand why error coming up, supplying data required.

here database:

enter image description here

and here json string:

{ "id":687, "firstname":"auto", "email":"auto", "eligible":true, "insertlogtime":"2015-07-21t00:51:59.917" } 

this server side code: apologies not including it. when step in debugger, members class initialised id.

// post api/members [responsetype(typeof(members))] public ihttpactionresult postmembers(members members) {     if (!modelstate.isvalid)         return badrequest(modelstate);      db.members.add(members);     db.savechanges();      return createdatroute("defaultapi", new { id = members.id }, members); } 

enter image description here

the members class, requested:

public partial class members {     public int id { get; set; }     public string firstname { get; set; }     public string email { get; set; }     public nullable<bool> eligible { get; set; }     public nullable<system.datetime> insertlogtime { get; set; } } 

below outermost exception stack trace...

   @ system.data.entity.internal.internalcontext.savechanges()    @ system.data.entity.internal.lazyinternalcontext.savechanges()    @ system.data.entity.dbcontext.savechanges()    @ xyz.service.controllers.memberscontroller.postmembers(members members) in c:\repositories\xyz\xyz.service\controllers\memberscontroller.cs:line 41    @ lambda_method(closure , object , object[] )    @ system.web.http.controllers.reflectedhttpactiondescriptor.actionexecutor.<>c__displayclass10.<getexecutor>b__9(object instance, object[] methodparameters)    @ system.web.http.controllers.reflectedhttpactiondescriptor.actionexecutor.execute(object instance, object[] arguments)    @ system.web.http.controllers.reflectedhttpactiondescriptor.executeasync(httpcontrollercontext controllercontext, idictionary`2 arguments, cancellationtoken cancellationtoken) 

i'd appreciate finding problem.

i want thank here help... great.

i found solution works after many hours of digging , given use s/o own reference, decided place here. if can think of alternative should have worked, i'd hear it.

here solution:

in ef autogenerated members class above:

public partial class members {     public int id { get; set; }     public string firstname { get; set; }     public string email { get; set; }     public nullable<bool> eligible { get; set; }     public nullable<system.datetime> insertlogtime { get; set; } } 

simply add line:

public partial class members {     [databasegenerated(databasegeneratedoption.none)]     public int id { get; set; }     public string firstname { get; set; }     public string email { get; set; }     public nullable<bool> eligible { get; set; }     public nullable<system.datetime> insertlogtime { get; set; } } 

i hope others find helpful, , again.


Comments