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 7565503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:10:19+00:00 2026-05-30T14:10:19+00:00

I have a model like public class User { [Key] public long UserId {

  • 0

I have a model like

public class User
{
    [Key]
    public long UserId { get; set; }

    [Required]
    public String Nickname { get; set; }

    public virtual ICollection<Town> Residencies { get; set; }

    public virtual ICollection<Town> Mayorships { get; set; }
}

and

public class Town
{
    [Key]
    public long TownId { get; set; }

    [Required]
    public String Name { get; set; }

    public virtual ICollection<User> Residents { get; set; }
    public virtual ICollection<User> Mayors { get; set; }
}

I was hoping EF would create two many to many relationships using automatically created TownResidents and TownMayors table. I can’t seem to find the necessary convention or explicit annotation to get this result.

Instead I am getting two FK UserIds in the Town table and two FK TownIds in the User table.

Any ideas how to get EF to see these at two many to many relationships?

Thanks

  • 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-05-30T14:10:20+00:00Added an answer on May 30, 2026 at 2:10 pm

    Well, EF doesn’t have some kind of word and grammar recognition algorithm which would be required to identify that you (probably) want that User.Residencies and Town.Residents form a pair of navigation properties and User.Mayorships and Town.Mayors form a second pair. Therefore it assumes that you have four one-to-many relationships and that each of the four navigation properties belongs to one of the relationships. (This is the reason for the four foreign keys you have seen in the database tables.)

    This standard assumption is not what you want, hence you must define the relationships explicitly to override this standard convention:

    Either with data annotations:

    public class User
    {
        [Key]
        public long UserId { get; set; }
    
        [Required]
        public String Nickname { get; set; }
    
        [InverseProperty("Residents")]
        public virtual ICollection<Town> Residencies { get; set; }
    
        [InverseProperty("Mayors")]
        public virtual ICollection<Town> Mayorships { get; set; }
    }
    

    Or with Fluent API:

    modelBuilder.Entity<User>()
        .HasMany(u => u.Residencies)
        .WithMany(t => t.Residents)
        .Map(x =>
        {
            x.MapLeftKey("UserId");
            x.MapRightKey("TownId");
            x.ToTable("TownResidents");
        });
    
    modelBuilder.Entity<User>()
        .HasMany(u => u.Mayorships)
        .WithMany(t => t.Mayors)
        .Map(x =>
        {
            x.MapLeftKey("UserId");
            x.MapRightKey("TownId");
            x.ToTable("TownMayors");
        });
    

    Fluent API has the advantage that you can control the name of the link table (and the key column names as well). You cannot define those names with data annotations.

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

Sidebar

Related Questions

I have a model like this defined... public class Product{ private long timestamp; //
I have the following code: modelBuilder.Entity<User>().HasMany(x => x.Items).WithRequired(); The model looks like public class
I have a model that looks like this: public class UserAdminEditViewModel { public User
I have a model that looks like this: public class Book { public string
I have a model class like following public class ProductModel { string ProductName {
I have a model that looks something like this public class User { public
I have a model class like this: class Note(models.Model): author = models.ForeignKey(User, related_name='notes') content
I have a model, smth like this: class Action(models.Model): def can_be_applied(self, user): #whatever return
This is how my data model for USER looks like public class User {
Yo i have the following nhibernate class: public class User { public virtual int

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.