如何获取linq中的最后一个插入ID

时间:2015-03-18 06:03:26

标签: c# linq asp.net-mvc-4

我有两个表,userstudentuser是父表,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表中获取最后一个插入useriduser表中的主键。

1 个答案:

答案 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;