I attempting to create a relational table between the ApplicationUser
and a table I've added Step
.
I think I'm a little confused about what ApplicationUser
is verse IdentityUser
verse the UserManager
, etc.
This is my attempt at the model.
public class UserStep
{
[ForeignKey("User")]
public string UserId { get; set; }
public int UserStepID { get; set; }
public int StepID { get; set; }
public virtual ApplicationUser User { get; set; }
public virtual Step Step { get; set; }
}
I've adjusted my model based on new information, however I still don't know if this is correct. I can pull an index of the usersteps with this:
var userSteps = db.UserSteps.Include(u => u.Step).Include(u => u.User);
return View(userSteps.ToList());
However my goal is to show only the steps associated to the current user. Any suggestions on how to change this to return only the current user?
Update
Within the controller for userstep, I have added this new method and created a view from it(which blows up). My goal is to get a list of usersteps associated to the current user. Is this not what you described in your update 2?
public ActionResult UserStep()
{
var user = UserManager.FindById(User.Identity.GetUserId());
var userStepsList = db.UserSteps.Where(u => u.UserId == user.Id);
return View(userStepsList);
}
Updated - Working Version
ApplicationUser Model in IdentityModel
Added
public virtual ICollection<UserStep> UserSteps { get; set; }
UserStep Model
public class UserStep
{
[ForeignKey("User")]
public string UserId { get; set; }
public int UserStepID { get; set; }
public int StepID { get; set; }
public virtual ApplicationUser User { get; set; }
public virtual Step Step { get; set; }
}
Step Model
Added
public virtual ICollection<UserStep> UserSteps { get; set; }
Created userstep controller and created a new method to view user specific relational table.
public ActionResult UserStep()
{
var user = UserManager.FindById(User.Identity.GetUserId());
var userStepsList = db.UserSteps.Where(u => u.UserId == user.Id).Include(u => u.Step).Include(u => u.User);
return View(userStepsList);
}
You can add the Step object directly to the ApplicationUser model as an extension.
If you open up IdentityModel.cs you can add properties like so:
public class ApplicationUser : IdentityUser
{
public virtual ICollection<Step> UserSteps { get; set; }
public async Task<ClaimsIdentity> blah blah
{
blah
}
}
Add an ID to the child object (referencing to the parent) if I were you, I would make the ApplicationUser the parent (meaning an ApplicationUser has one or many UserSteps) by adding the property public int UserId{get;set;}
so that when you need to get a particular users UserStep you can do so by querying the db.Steps with the User's ID.
Hope this helps, comment with questions I'll try and check back tomorrow early.
Update 1:
The way you have the user reference to ApplicationUser is close to what you are going to want to add to Step. Make it look more like this though...(in your Step object)
public int UserID { get; set; }
[ForeignKey("UserID")]
public virtual ApplicationUser User { get; set; }
I noticed your [ForeignKey("")] declarations were different from how I impliment mine, I'm not sure if your way is wrong, however I know my way works for me (having the [ForeignKey()] on the navigation property, not the actual model property)
I remember my first relationship with applicationuser was really tricky, but it's so important and gratifying when you get it.
Update 2
In response to your comment...
You will grab a reference to a user by something like :
var user = db.Users.Where(u => u.Id = parameterID)
parameterID being the id passed to whatever ActionResult you have GETing your collection of UserSteps. If you wanted to just get the current Users ID, try something like:
var user = UserManager.FindById(User.Identity.GetUserId());
Then you just query for the list of userSteps from that User's ID
var userSteps = db.UserSteps.Where(u => u.UserID == user.Id)
you could then return View(userSteps) if you have a view directly binding for a list of UserStep objects (which is how I would display these if I were you).
Hope this works out for you, I'll check back again later.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With