asp.net mvc - Getting key from new record before calling SaveChanges in MVC not using EF -


i using mvc5 without entity framework, , want retrieve key newly inserted record before calling savechanges. because need key work foreign key in different record.

here's pseudo code better explain want do:

1) create record person  2) var newpersonid = newly created personid person record  3) create record employee, set personid (foreign key) equal newpersonid  4) person.savechanges    emplyee.savechanges 

thus, problem step 2.

i read way when using entity framework setup relationsship in model [foreignkey("xxx")], , entity framework take of things. don't use entity framework, how can this?

if not using ef how come have savechanges.

ok, way can use transactionscope.

using (transactionscope transactionscope = new transactionscope()){      //some other code....       person.savechanges();      employee.personid = person.id;      employee.savechanges()       //if exception rised here, save changes reverted      transactionscope.complete();     } 

and need have system.transactions included


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -