umair mohsin

umair mohsin

  • 1.3k
  • 356
  • 56.4k

Db.Savechanges not saving data

Jan 27 2024 4:22 PM

i am using aspnet identity and entering records to another table. here is the code.it is in default account controller    

public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                can.Email = model.Email;
                can.FirstName = model.FirstName;
                can.LastName = model.LastName;
                if (model.Gender == "1")
                    can.Gender = true;
                else
                    can.Gender = false;
                db.Candidates.Add(can);
                db.SaveChanges();

                context.Insert(can);

                context.Save();

                var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName };
                var result = await UserManager.CreateAsync(user, model.Password);
                var rolestore = new RoleStore<IdentityRole>();
                var rolemanager = new RoleManager<IdentityRole>(rolestore);
                //adding Jobseeker to table can be put in a seperate function

                if (result.Succeeded)
                {
                    string role = "can";
                    IdentityRole rol = new IdentityRole { Name = role };

                    if (!rolemanager.RoleExists(role))
                    {
                        IdentityResult res = RoleManager.Create(rol);
                        await UserManager.AddToRoleAsync(user.Id, role);
                        await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                    }
                    else
                    {
                        await UserManager.AddToRoleAsync(user.Id, role);

                        await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                    }
                    ViewData["Name"] = user.FirstName;
                    return RedirectToAction("Index", "JS", can);
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

db.Savechanges();

using control to dispose method which is this

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_userManager != null)
                {
                    _userManager.Dispose();
                    _userManager = null;
                }

                if (_signInManager != null)
                {
                    _signInManager.Dispose();
                    _signInManager = null;
                }
            }

            base.Dispose(disposing);
        }

db   is my another datacontext initialize in default account controller

don't know how to handle this please guide accordingly 


Answers (2)