简体   繁体   中英

Save new record in Entity Framework

I have a litte problem about save new record to database by using Entity framework.

Here the code:

using (_context)
                {
                    _context.AddToStudent(newStudent);
                    _context.AddToStudentInfo(newStudentInfo);
                    _context.SaveChanges();                   
                    return true;
                }

My question is : If 1 of 2 _context.AddTo fail so the SaveChanges will be terminated or it will save the one success ??? I appreciate any answer :)

SaveChanges() will either execute all the unexecuted operations on the database or none of them (if for example there is an error). Thus in this case if there is an error in the newStudentInfo object (such as a required property not being set) then neither newStudentInfo or newStudent will be saved to the database.

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