简体   繁体   中英

Looking up a user by id (guid) from within a view in MVC5

I'm building an invitation based registration system for our site. Essentially the user goes to a page, types in an email, selects a name from a dropdown and hits submit. The link is then emailed to the specified email for the user to register.

I'm running into some issues creating this entire subsystem. Essentially my database table has 5 columns validation_id - uniqueidentifier(guid), all_id - foreign key to my employees table, generated_by - a uniqueidentifier that links to AspNetUsers and gets the user's id, generated_on - the date, email - the new user's email.

Basically the invitation table has foreign key relationships with two other tables, using all_id and generated_by . I need to be able to retrieve information from both tables for registration later.

My model for registration_invitations looks like:

public partial class registration_invitations
{
    [Key]
    public Guid validation_id { get; set; }

    public int all_id { get; set; }

    [StringLength(128)]
    public string generated_by { get; set; }

    [UIHint("Date")]
    [Column(TypeName = "date")]
    public DateTime? generated_on { get; set; }

    [Display(Name="Email")]
    public string email { get; set; }
}

The create view saves everything exactly as I need, but I can't figure out how to get the user that generated the invitation by their id, and I can't seem to get the information from the all_employees table.

Any ideas?

Let me see if i got this correctly.

The generated_by property should points to a foreign key reference to your Users table, so this way EF will generate the right public User generated_by property and let you access to the information you want.

Is that what you want to achieve?

EDIT: You only have to create your tables with the foreign key references in sql server like this:

all_id INT FOREIGN KEY REFERENCES all_employees(all_id)
generated_by INT FOREIGN KEY REFERENCES AspNetUsers(id)

Then Entity Framework do all the job creating the referenced properties you need

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM