none
How can I connect with the AspNetUsers table in MVC 5?

    Question

  • I have a model, in which I have used several foreign keys from other tables. The point is that I also need to get the Id of the user, from the AspNetUsers table, though, I don't know how. I have done something like this, but it doesn't seem to be working:

    public class Message
    {
        public int MessageID { get; set; }

        [Required(ErrorMessage="Please type a message to submit")]
        [DataType(DataType.MultilineText)]
        [Display(Name="Message")]
        public string MessageText { get; set; }

        public DateTime WrittenOn { get; set; }

        [Required(ErrorMessage="Please select a ticket")]
        [ForeignKey("Ticket")]
        [Display(Name="Ticket")]
        public int TicketID { get; set; }

        public virtual Ticket Ticket { get; set; }

        [Required(ErrorMessage = "Please select a business")]
        [ForeignKey("Business")]
        [Display(Name = "Business")]
        public int BusinessID { get; set; }

        public virtual Business Business { get; set; }

        [Required(ErrorMessage = "Please select a user")]
        [ForeignKey("aspnetusers")]
        [Display(Name = "User")]
        public int Id { get; set; }

        public virtual ApplicationUser ApplicationUser { get; set; }
    }

    When I try to create a controller using the Entity Framework with my model, I get an error message saying:

        The ForeignKeyAttribute on property 'Id' on type ... is not valid. The navigation property 'aspnetusers' was not found on the dependent type ... The Name value should be a valid navigation property name.

    If someone could help me solve the problem, I would be glad.
    Friday, August 22, 2014 10:48 AM

All replies