i have following table in sql server 2008 database:
user -------------------------------------------------------- id numeric(18, 0) | identity(1, 1) pk not null name nchar(20) | not null
i'm using ado.net entity data model do:
myentities entities; try { entities = new myentities(); if (entities.user.count(u => u.name == username) == 0) { entities.user.addobject(new user() { name = username }); resultcode = 1; entities.savechanges(); } else { resultcode = 2; } } catch { resultcode = 3; } { if (entities != null) entities.dispose(); }
how can user.id new user added?
try keeping reference user object. once savechanges() called, id should automatically updated. here's modification of code demonstrate:
if (entities.user.count(u => u.name == username) == 0) { user newuser = new user() { name = username }; entities.user.addobject(newuser); resultcode = 1; entities.savechanges(); // newuser.id should populated @ point. }
Comments
Post a Comment