locked
How to assign ASP.NET Identity User Id as foreign key to another table? RRS feed

  • Question

  • User1730047983 posted

    I'm trying to design a controller that allows my admin user to create project and assign it to an Identity User.

    However I'm getting stumped on how to tell the controller to get that specific user's Id and assign it to the projects table.

    // POST: /Admin/ClientProjects/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create([Bind(Include="Id,Name,ProjectStatusId")] ClientProject clientproject, string userId)
    {
        var client = UserManager.FindById(userId);
     
        if (ModelState.IsValid)
        {
            clientproject.User = client;
            Db.ClientProjects.Add(clientproject);
            await Db.SaveChangesAsync();
            return RedirectToAction("Index");
        }
     
        ViewBag.ProjectStatusId = new SelectList(Db.ProjectStatuses, "Id""Name", clientproject.ProjectStatusId);
        return View(clientproject);
    }
    
    Sunday, April 6, 2014 3:16 PM

Answers

  • User-1454326058 posted

    Hi akalempa,

    Based on your code, I think the relation of ApplicationUser and ClientProject is one to many.

    For that scenario, the code should be:

     

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create([Bind(Include="Id,Name,ProjectStatusId")] ClientProject clientproject, string userId)
    {
        var user = new ApplicationUser();
        if (ModelState.IsValid)
        {
            user.ClientProjects.Add(clientproject);
            var result = await UserManager.CreateAsync(user);
                    if (result.Succeeded)
                    {
                        
                         return RedirectToAction("Index");               
                    }
                    else
                    {
                        //TODO 
                    }
        }
     
        ViewBag.ProjectStatusId = new SelectList(Db.ProjectStatuses, "Id", "Name", clientproject.ProjectStatusId);
        return View(clientproject);
    }
    

    After add the clientproject to the ApplicationUser it will set the foreign key value automatically.

    Thanks

    Best Regards              

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 8, 2014 2:01 AM

All replies

  • User386835355 posted

    you must define relation between Identity User and ClientProjects model.

    you can do this as below

    public class ClientProjects
    {
    //Other Model properties
    
    public string UserId {get; set;}
    
    [ForeignKey("UserId")]
    public virtual ApplicationUser ApplicationUser {get; set;}
    }

    Sunday, April 6, 2014 3:56 PM
  • User1730047983 posted

    My current relation is this, the EF knows understands the relation.

    public class ApplicationUser : IdentityUser
    {
        public virtual ICollection<ClientProject> ClientProjects { getset; }
    }

    public class ClientProject
    {
        public int Id { getset; }
     
        [Display(Name = "Project")]
        [Required(ErrorMessage = "A project name is required.")]
        public string Name { getset; }
     
        // ForeignKey => dbo.IdentityUser
        [Display(Name = "Client")]
        [Required(ErrorMessage = "Please select a client account to associate with.")]
        public virtual ApplicationUser User { getset; }
    }
    Sunday, April 6, 2014 4:13 PM
  • User-1454326058 posted

    Hi akalempa,

    Based on your code, I think the relation of ApplicationUser and ClientProject is one to many.

    For that scenario, the code should be:

     

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create([Bind(Include="Id,Name,ProjectStatusId")] ClientProject clientproject, string userId)
    {
        var user = new ApplicationUser();
        if (ModelState.IsValid)
        {
            user.ClientProjects.Add(clientproject);
            var result = await UserManager.CreateAsync(user);
                    if (result.Succeeded)
                    {
                        
                         return RedirectToAction("Index");               
                    }
                    else
                    {
                        //TODO 
                    }
        }
     
        ViewBag.ProjectStatusId = new SelectList(Db.ProjectStatuses, "Id", "Name", clientproject.ProjectStatusId);
        return View(clientproject);
    }
    

    After add the clientproject to the ApplicationUser it will set the foreign key value automatically.

    Thanks

    Best Regards              

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 8, 2014 2:01 AM