MVC Code first data design

Natavia Finnie
Natavia Finnie used Ask the Experts™
on
I am trying to create joins between 3 tables using MVC code first approach. My main table is the Request table. I need to be able to join these tables.  The requirements is going to build a multi check list box where multiple requirements may be checked. A request may have multiple requirements... I need help with the navigational properties

public partial class Request
    {
        public int ID { get; set; }

        [StringLength(10)]
        [Display(Name = "User Name")]
        public string UserName { get; set; }

       public DateTime? RequestDate { get; set; }

        public int? RequestRequirementId { get; set; }
    }

Open in new window

==================================================================================
public partial class Requirement
    {
        public int ID { get; set; }

        [Required]
        [StringLength(100)]
        public string Description { get; set; }
    }

Open in new window

===================================================================================
public partial class RequestRequirement
    {  
        public int ID { get; set; }
       
        public int RequestId { get; set; }
        
        public int RequirementId { get; set; }

        public virtual Requirement Requirement { get; set; }

        public virtual Request Request { get; set; }
    }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Duy PhamFreelance IT Consultant

Commented:
I think you could use Linq Join in this case. See reference here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/join-clause.
Natavia FinnieSenior Analyst Technical Focus Applications

Author

Commented:
I need help with the navigational properties
Freelance IT Consultant
Commented:
Oh, sorry that I didn't see the last sentence.

Maybe this:
public partial class Request
{
    [Key]
    public int ID { get; set; }
    ...
}

public partial class Requirement
{
    [Key]
    public int ID { get; set; }
    ...
}

public partial class RequestRequirement
{
    public int ID { get; set; }
    public int RequestId { get; set; }
    public int RequirementId { get; set; }

    [ForeignKey("RequestId")]
    public Request Request { get; set; }

    [ForeignKey("RequirementId")]
    public Requirement Requirement { get; set; }
}

Open in new window

Natavia FinnieSenior Analyst Technical Focus Applications

Author

Commented:
Thank you!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial