如何在LINQ中获取最后插入的ID

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

如何从user表中获取最后插入的ididuser表中的主键。

1个回答

7
您可以按照以下方式获取它:
// I suppose that the corresponding's property name is ID.
// If it is not, you should change it correspondingly.
stud.studentID = u.ID;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接