Q:

Insert users Id into multiple tabels using MVC Identity

I want to insert user id to multiple tables after registration success
public async Task<ActionResult> Register(RegisterViewModel model , RoleViewModel role )
    {
        if (ModelState.IsValid)
        {
            ApplicationDbContext context = new ApplicationDbContext();
            var user = new ApplicationUser {
                UserName = model.UserName,};

            Activity activity = new Activity
            {
                RoleId = model.RoleName,
                UserId = model.Id,
            };
            entities.activities.Add(activity);
            entities.SaveChanges();

            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded )
            {
                result = await UserManager.AddToRolesAsync(user.Id, model.RoleName);

                await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                return RedirectToAction("Index", "Home");
            }

            AddErrors(result);
        }

This is AcountController , no thing happen I want to insert Id from users table to UserId in Activity Table

A:

You have at least two options for this
First.
if you are using Code First, you can create Your Activity class together with the ApplicationUser class. see how here https://go.microsoft.com/fwlink/?LinkID=317594
Second.
You can retrieve the user id just the way you did it here
result = await UserManager.AddToRolesAsync(user.Id, model.RoleName);
var currentId = user.Id;

Also your Activity insert has to come after the user has been created
just as the code below
public async Task<ActionResult> Register(RegisterViewModel model, RoleViewModel role)
{
   if (ModelState.IsValid)
   {
       ApplicationDbContext entities = new ApplicationDbContext();
       var user = new ApplicationUser {UserName = model.UserName};            

       var result = await UserManager.CreateAsync(user, model.Password);
       if (result.Succeeded )
       {
           Activity activity = new Activity
           {
               RoleId = model.RoleName,
               UserId = user.Id,
           };
           entities.activities.Add(activity);
           entities.SaveChanges();
           result = await UserManager.AddToRolesAsync(user.Id, model.RoleName);

           await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

           return RedirectToAction("Index", "Home");
       }

       AddErrors(result);
   }
}

