我有两个表,user
和student
。
user
是父表,student
是子表
所以从以下代码
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??
}
}
如何从id
表中获取最后一个插入user
?
id
是user
表中的主键。
答案 0 :(得分:2)
你可以得到如下:
// I suppose that the corresponding's property name is ID.
// If it is not, you should change it correspondingly.
stud.studentID = u.ID;