How to include ApplicationUser in relational table / model in asp.net mvc

I am trying to create a relational table between ApplicationUser and the table that I added Step .

I think I'm a little confused by the fact that ApplicationUser is an IdentityUser verse UserManager verse, etc.

This is my attempt at a 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 adjusted my model based on new information, however I still don't know if this is correct. I can derive an index of user steps using 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 with the current user. Any suggestions on how to change this to only return the current user?

Update

Inside the controller for userstep, I added this new method and created a view from it (which explodes). My goal is to get a list of user steps related 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 a user controller and created a new method for viewing a 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); } 
+5
source share
1 answer

You can add a Step object directly to the ApplicationUser model as an extension.

If you open IdentityModel.cs, you can add the following properties:

  public class ApplicationUser : IdentityUser { public virtual ICollection<Step> UserSteps { get; set; } public async Task<ClaimsIdentity> blah blah { blah } } 

Add an identifier to the child (referring to the parent), if I were you, I would make ApplicationUser the parent (this means that ApplicationUser has one or more UserStep) by adding the public int UserId{get;set;} property so that when If you need to get specific UserStep users, you can do this by querying db.Steps with the user ID.

Hope this helps, please comment on the questions. I will try and come back tomorrow.

Update 1:

The way you reference the ApplicationUser application is close to what you want to add to the 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 that your [ForeignKey ("")] ads were different from the way I apply mine, I'm not sure your path is wrong, however I know that my path works for me (having [ForeignKey ()] on property navigation, not the actual property of the model)

I remember that my first relationship with applicationuser was really complicated, but it is so important and nice when you received it.

Update 2

In response to your comment ...

You will receive a link to the user, for example:

 var user = db.Users.Where(u => u.Id = parameterID) 

parameterID, which is the identifier passed to any ActionResult, you have GETing for your UserStep collection. If you just want to get the current user id, try something like:

 var user = UserManager.FindById(User.Identity.GetUserId()); 

Then you just query the userStep list from that user id

 var userSteps = db.UserSteps.Where(u => u.UserID == user.Id) 

you can return View (userSteps) if you have a binding directly to the list of UserStep objects (so I would display them if I were you).

Hope this works for you, I'll be back later.

+3
source

Source: https://habr.com/ru/post/1242660/


All Articles