Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9130411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:57:40+00:00 2026-06-17T07:57:40+00:00

Ok, I have been pulling out my hair because I simply cannot make a

  • 0

Ok, I have been pulling out my hair because I simply cannot make a many to many relationship. I have the following two models:

public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string CompanyName { get; set; }
    public string FirstName { get; set; }
    public string Lastname { get; set; }
    public string EmailAddress { get; set; }
    public string PhoneNumber { get; set; }
    public bool? ChangePassword { get; set; }
    public bool? Deletable { get; set; }

    //Add more Properties for more fields

    public virtual IQueryable<CompanyInformation> ParentCompany { get; set; }
}

and

public class CompanyInformation
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int id { get; set; }

    [DisplayName("Company Name:")]
    public string companyName { get; set; }

    [DisplayName("Website Address:")]
    [Url(ErrorMessage="The Website field is not a valid fully-qualified http, https, or ftp URL. (Example: http://www.website.com)")]
    public string website { get; set; }
    public string contactTitle { get; set; }

    [DisplayName("Contact First Name:")]
    public string contactFirstName { get; set; }
    //[Required]
    [DisplayName("Contact Last Name:")]
    public string contactLastName { get; set; }

    [Phone]
    [DisplayName("Phone Number:")]
    public string contactPhoneNumber { get; set; }

    [DisplayName("Address Display?")]
    public bool displayAddress { get; set; }
    [DisplayName("Phone Number?")]
    public bool displayPhoneNumber { get; set; }

    [DisplayName("Address 1:")]
    public string address1 { get; set; } 
    [DisplayName("Address 2:")]
    public string address2 { get; set; }

    [DisplayName("City:")]
    public string city { get; set; }

    [DisplayName("State:")]
    public string state { get; set; }

    [DisplayName("Zip/Postal Code:")]
    public string zipCode { get; set; }

    [DisplayName("Search Engine?")]
    public bool allowSearchEngines { get; set; }


    //Navigation Property
    public virtual IQueryable<UserProfile> CompanyUsers{ get; set; }

}

I’m trying to make a many-to-many relationship between these two and I just can’t figure out how to do it properly. I should mention that I am very new to the EF Code First.
My Context Class looks like the following:

public class myDB : DbContext
{
    public SchedulerDB()
        : base("DefaultConnection")
    {

    }

    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<CompanyInformation> Companies { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<UserProfile>().HasMany(e => e.ParentCompanies).WithMany(e => e.CompanyUsers);            
    }

}

ok, As soon as I add the modelBuilder above I get the following error:

The type arguments for method 'System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Scheduler.Model.UserProfile>.HasMany<TTargetEntity>(System.Linq.Expressions.Expression<System.Func<Scheduler.Model.UserProfile,System.Collections.Generic.ICollection<TTargetEntity>>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.  C:\Users\Hiva\Documents\Project\ToDo\Infrastructure\myDB.cs

What am I doing wrong? I can’t seem to find any examples that use the modelBuilder differently to achieve a many-to-many relationship between two tables. Thank you in advanced for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:57:41+00:00Added an answer on June 17, 2026 at 7:57 am

    You should use ICollection for navigation properties:

    ICollection<UserProfile> CompanyUsers{ get; set; }
    

    and

    ICollection<UserProfile> ParentCompanies{ get; set; }
    

    instead of IQueriable

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been pulling my hair out trying to make this work. I have
I have been pulling my hair out because of this. My cucumber step clicks
I have been pulling my hair out because of this 1 line of code.
I have been pulling my hair out about this since Friday and I am
I have been pulling my hair out trying to figure out what I can't
I've been pulling out my hair trying to figure this problem out. I have
I have been pulling my hair trying to figure this out but nothing is
I have been pulling my hair out over the past few days with a
Have been pulling out my hair trying to find out why my sessions are
I have been pulling my hair out trying to solve this. What I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.