简体   繁体   中英

how to get the last insert ID in linq

I have two tables, user and student . user is the parent table and student is the child table

so from the following code

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??


            }     
        }

how can i get the last insert id from the user table? id is the primary key in user table.

You could get it as below:

// I suppose that the corresponding's property name is ID.
// If it is not, you should change it correspondingly.
stud.studentID = u.ID;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM