繁体   English   中英

检索MVC 4中最后插入记录的自动增量ID

[英]Retrieve auto increment id of last inserted record in MVC 4

MVC 4对我来说是一个非常难以破解的难题。 我在SQL Compact数据库中使用三个表。

Accounts表首先保存(我已经完成),但PhonesData表使用AccountsID作为外键。 MVC 4通过ModelView完成所有数据库的工作,那么如何通过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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM