Reputation: 41
MVC 4 has been a very tough nut to crack for me. I am using three tables in a SQL Compact database.
Accounts
Table is saved first (which I have done) but the Phones
and Data
table use AccountsID
as a foreign key. MVC 4 does all the database work through the Model
and the View
so how can I get AccountsID
from the newly added row in the Database through MVC 4 and provided it to the Phones and Data Models so everything saves correctly?
Upvotes: 3
Views: 4288
Reputation: 5573
You can do the following:
var account = new Account();
context.Accounts.Add(account);
context.SaveChanges();
var Phone = new Phone();
// account.Id will be populated with the Id from the Database.
// as long as it's the identity seed or set to NewID()(Sql Server)
phone.AccountId = account.Id;
...
Upvotes: 9