我知道有很多关于这方面的话题,因为我已经搜索了它,但据我所知,它们都不能适合/使用我的代码。我只是想在插入后从我的数据库返回一个自动生成的ID。这是我的代码的一部分:
using (***LINQDataContext dc = new ***LINQDataContext())
{
dc.Users.InsertOnSubmit(new User
{
Username = user,
Password = EncryptPassword(user, pass, 1, null, null),
Email = email
});
dc.SubmitChanges();
return true;
}
答案 0 :(得分:8)
var newUser = new User {
Username = user,
Password = EncryptPassword(user, pass, 1, null, null),
Email = email
};
dc.Users.InsertOnSubmit(newUser);
dc.SubmitChanges();
// any identity / rowversion properties should now have values
var id = newUser.Id;