![](/img/trans.png)
[英]Last Inserted Auto Increment Id using ASP.NET MVC & Entity Framework
[英]Retrieve auto increment id of last inserted record in MVC 4
MVC 4对我来说是一个非常难以破解的难题。 我在SQL Compact数据库中使用三个表。
Accounts
表首先保存(我已经完成),但Phones
和Data
表使用AccountsID
作为外键。 MVC 4通过Model
和View
完成所有数据库的工作,那么如何通过MVC 4从数据库中新添加的行中获取AccountsID
并将其提供给Phones和Data Models,以便一切正确保存?
您可以执行以下操作:
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;
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.