Đức Bùi
Đức Bùi

Reputation: 527

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 :)

Upvotes: 4

Views: 2877

Answers (1)

Judo
Judo

Reputation: 5247

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.

Upvotes: 3

Related Questions