Search code examples
c#asp.net-mvcentity-framework-6identity

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


Here is my sample code.

Firstly I want to return the Id (institutionId) of the saved record

The Ownerinfo method accepts int institutionId in order to associate the record with the registration institution.

Please kindly assist on the best way to achieve this:

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);
    }

Solution

  • You can get the inserted identity by int id = institution InstitutionId. EF gives you this when you insert record to a table.

    Getting the Max value of Identity is not the right way. because some other application might have inserted or deleted records from the table meanwhile.