如何在linq中获取最后一个插入ID
how to get the last insert ID in linq
我有两个 table,user
和 student
。
user
是 parent table 而 student
是 child table
所以来自下面的代码
public void adduserStudenttype(StudentAddUserModel s)
{
using (DASEntities db = new DASEntities())
{
user u = new user();
u.usersEmail = s.Email;
u.usersPassword = s.Password;
u.usersFname = s.Fname;
u.usersUtype = s.UserType;
db.user.Add(u);
db.SaveChanges();
student stud = new student();
stud.studentID = \ how to get the id in the user last insert??
}
}
如何从 user
table 中获取最后插入的内容 id
?
id
是 user
table 中的主键。
您可以通过以下方式获取:
// I suppose that the corresponding's property name is ID.
// If it is not, you should change it correspondingly.
stud.studentID = u.ID;
我有两个 table,user
和 student
。
user
是 parent table 而 student
是 child table
所以来自下面的代码
public void adduserStudenttype(StudentAddUserModel s)
{
using (DASEntities db = new DASEntities())
{
user u = new user();
u.usersEmail = s.Email;
u.usersPassword = s.Password;
u.usersFname = s.Fname;
u.usersUtype = s.UserType;
db.user.Add(u);
db.SaveChanges();
student stud = new student();
stud.studentID = \ how to get the id in the user last insert??
}
}
如何从 user
table 中获取最后插入的内容 id
?
id
是 user
table 中的主键。
您可以通过以下方式获取:
// I suppose that the corresponding's property name is ID.
// If it is not, you should change it correspondingly.
stud.studentID = u.ID;