我有一个将数据保存到数据库的 post 方法,我需要 return 保存记录的 ID 并将其作为外键传递给另一个方法

I have a post method that saved data to database, I need to return the id of the saved record and pass it another method as a Foreigh Key

这是我的示例代码。

首先我想return保存记录的Id (institutionId)

Ownerinfo 方法接受 int institutionId 以便将记录与注册机构相关联。

请提供实现此目的的最佳方法:

public ActionResult Registration(RegistrationViewModel model)
    {
        if (model != null)
        {
            string userId = User.Identity.GetUserId();

            if (model.ProviderTypeId == 1) // Private FET College
                    {
                        var institution = new Institution
                        {
                            ProviderTypeId = model.ProviderTypeId,
                            RegisteredName = model.RegisteredName,
                            CIPCNo = model.CIPCNo,
                            ExamCentreNumber = model.ExamCentreNumber,
                            DateCreated = DateTime.Now,
                            CreatedById = Int32.Parse(userId)
                        };

                        _db.Institutions.Add(institution);
                        _db.SaveChanges();
                        int institutionId = _db.Institutions.Max(item => item.InstitutionId);
            }

            if (model.ProviderTypeId == 2) // Private AET College
                    {
                        var institution = new Institution
                        {
                            ProviderTypeId = model.ProviderTypeId,
                            RegisteredName = model.RegisteredName,
                            CIPCNo = model.CIPCNo,
                            ExamCentreNumber = model.ExamCentreNumber,
                            DateCreated = DateTime.Now,
                            CreatedById = Int32.Parse(userId)
                        };
                        _db.Institutions.Add(institution);
                        _db.SaveChanges();
                        int institutionId = _db.Institutions.Max(item => item.InstitutionId);
                     }

            if (model.ProviderTypeId == 3) // Independent School
                    {
                        var institution = new Institution
                        {
                            ProviderTypeId = model.ProviderTypeId,
                            RegisteredName = model.RegisteredName,
                            CIPCNo = model.CIPCNo,
                            ExamCentreNumber = model.ExamCentreNumber,
                            RegistrationNo = model.RegistrationNo,
                            RegisteredWithDbe = model.RegisteredWithDbe,
                            CreatedById = Int32.Parse(userId),
                            DateCreated = DateTime.Now
                        };

                        _db.Institutions.Add(institution);
                        _db.SaveChanges();

                        int institutionId  = _db.Institutions.Max(item => item.InstitutionId);
                    }

            return RedirectToAction("Ownerinfo");
        }
        return View(model);
    }

您可以通过int id = institution InstitutionId获取插入的身份。当您将记录插入 table.

时,EF 会为您提供此信息

获取Identity的最大值不是正确的方法。因为某些其他应用程序可能同时从 table 中插入或删除了记录。