i am using aspnet identity and entering records to another table. here is the code.it is in default account controller
public async Task
{
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
var rolemanager = new RoleManager
//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
umair mohsinPosted Jan 27, 2024, 6:34 PM
My table name is candidate not application user
and try catch block throws error
object refrence not set to aninstance of an object error. cant handle this error.in my understanding everything is fine
i am inserting record through my own repository not identity repository
Jithu ThomasPosted Jan 27, 2024, 5:45 PM
It looks like you're trying to register a user using ASP.NET Identity and at the same time, add some information to another table (Candidates). However, there are a few things in your code that need attention.
Context Management:
It's crucial to manage the context properly, especially when dealing with multiple contexts. Make sure to dispose of your db and context appropriately.
Role Management:
It seems like you are creating a role each time a user is registered. You might want to consider creating roles only once and then assigning them to users as needed.
Database Transactions:
Consider wrapping your database operations in a transaction to ensure that either all operations are completed successfully, or none are.
Based on the above points I have made some updates in the code as follows: -
Also please note the following changes:
I added a using statement to ensure that the transaction is properly disposed of, even if an exception occurs.
I wrapped your database operations in a transaction (BeginTransaction, Commit, and Rollback).
I modified the role creation part to use asynchronous methods.
Additionally, handle exceptions appropriately, and consider logging to help diagnose any issues.
Please accept the answer if you found it is useful.....