sql server 2008 - EF insert not follow db auto increment(DB First) -
.edmx
<property name="uid" type="int" nullable="false" storegeneratedpattern="identity" />
ef had set storegeneratedpattern = "identity", why still cannot save db? debug show value 0, guess quite correct server not return value web before inserting, model validate false, , had go through c#, entity framework, auto increment , autonumber entity framework no luck, doing wrong? idea?
update 1:
added model , meta data picture, forgot mention im using ef db first generate class
asp.net mvc view
<div class="editor-field"> @html.hiddenfor(model => model.uid) </div>
asp.net mvc controller
[httppost, validatejsonantiforgerytoken] public jsonresult create(basicuserinfo basicuserinfo) { if (modelstate.isvalid) { db.basicuserinfoes.add(basicuserinfo); db.savechanges(); } else { return json(new { valid = "n", err = captureerror() }); } return json(new { valid = "y", route = url.action("index") }); }
script
var self = this; var data = ""; var _model = new model(); var data = json.stringify({ "basicuserinfo": $(e.currenttarget).serializeobject() }); var _create = new create(); _create.fetch({ type: 'post', headers: headers, data: data, contenttype: "application/json; charset=utf-8", success: function (data, textstatus, jqxhr) { if (textstatus.valid == "y") { _model.appendaddsuccess(textstatus.route); } else { _model.appendfailed(textstatus.err); } }, error: function (jqxhr, textstatus, errorthrown) { console.log(textstatus.statustext); } });
sql server 2008
error
modal
metadata
it looks entity framework not recognizing property table field. when creating entity, shouldn't need specify html helper primary key if set isidentity = true. if ef recognizes entity, automatically generate primary key field. therefore need html helpers other fields.
make sure model property same name database column. in case, db column should titled uid
looks like. can specify target column in model using column
attribute if don't want change db tables.
Comments
Post a Comment