c# - sqlexception cannot insert duplicate key row -
tried alter sql statement change format of how user names create. creating dummy data testing on webpage need multiple users. want change name user student{integer}@email.com , wanted change name student{integer}
error
sqlexception: cannot insert duplicate key row in object 'dbo.users' unique index 'ux_users'. duplicate key value (1, student0@email.com). statement has been terminated.
sql statement
string basestring = "student{0}@email.com"; string username = "student{0}"; using (database dc = new database()) { dc.connection.open(); (int = 0; < 400; i++) { string email = string.format(basestring,i); string sname = string.format(username, i); user u = new user(){ name = sname, username = email, institutionuniqueid = email, email = email, creationdate=datetime.now, institutionsid=1, guid= guid.newguid() }; dc.users.insertonsubmit(u); } dc.submitchanges(system.data.linq.conflictmode.failonfirstconflict); } this getting error:
dc.submitchanges(system.data.linq.conflictmode.failonfirstconflict); i using linqpad test out.
change code this:
string basestring = "student{0}@email.com"; string username = "student{0}"; using (database dc = new database()) { (int = 0; < 400; i++) { dc.connection.open(); string email = string.format(basestring,i); string sname = string.format(username, i); user u = new user(){ name = sname, username = email, institutionuniqueid = email, email = email, creationdate=datetime.now, institutionsid=1, guid= guid.newguid() }; dc.users.insertonsubmit(u); dc.submitchanges(system.data.linq.conflictmode.failonfirstconflict); dc.connection.close(); } } you need place submit changes inside loop
best regards
Comments
Post a Comment