简体   繁体   English

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

[英]how to get the last insert ID in linq

I have two tables, user and student . 我有两个表, userstudent user is the parent table and student is the child table user是父表, student是子表

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? 如何从user表中获取最后一个插入id id is the primary key in user table. iduser表中的主键。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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