c# - How to generate and auto increment Id with Entity Framework -


revised entire post.

i'm trying post following json post request via fiddler:

{username:"bob", firstname:"foo", lastname:"bar", password:"123", headline:"tuna"} 

however i'm getting error:

message "cannot insert value null column 'id', table 'xxx_f8dc97e46f8b49c2b825439607e89b59.dbo.user'; column not allow nulls. insert fails.\r\nthe statement has been terminated." string 

though if manually send random id along request good. so:

{id:"1", username:"bob", firstname:"foo", lastname:"bar", password:"123", headline:"tuna"} 

why entity framework not generate , auto increment id's? poco class follows:

public class user {     [key]     [databasegenerated(databasegeneratedoption.identity)]     public string id { get; set; }     public string username { get; set; }     public string firstname { get; set; }     public string lastname { get; set; }     public string password { get; set; }     public string headline { get; set; }     public virtual icollection<connection> connections { get; set; }     public virtual icollection<address> addresses { get; set; }     public virtual icollection<phonenumber> phonenumbers { get; set; }     public virtual icollection<email> emails { get; set; }     public virtual icollection<position> positions { get; set; } }  public class connection {     public string connectionid { get; set; }     public int userid { get; set; }     public virtual user user { get; set; } }  public class phonenumber {     public string id { get; set; }     public string number { get; set; }     public int cycle { get; set; }     public int userid { get; set; }     public user user { get; set; } } 

here controller method. when in debug mode , send request via fiddler breaks @ db.savechanges(); , gives error seen bit above.

    // post api/xxx/create     [actionname("create")]     public httpresponsemessage postuser(user user)     {         if (modelstate.isvalid)         {             db.users.add(user);             db.savechanges();              httpresponsemessage response = request.createresponse(httpstatuscode.created, user);             response.headers.location = new uri(url.link("defaultapi", new { id = user.id }));             return response;         }         else         {             return request.createerrorresponse(httpstatuscode.badrequest, modelstate);         }     } 

what's wrong?

solution

change string id int instead , remove data annotations. renamed id userid, still following convention, , made changes necessary in other poco's match changes.

this guess :)

is because id string? happens if change int?

i mean:

 public int id { get; set; } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -