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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:25:06+00:00 2026-06-17T09:25:06+00:00

I am using EF5 (Code 1st) and doing all my configurations with the Fluent

  • 0

I am using EF5 (Code 1st) and doing all my configurations with the Fluent API. My model looks like this:

public class AddressType    
{
    public int    AddressTypeID { get; set; }
    public string Name          { get; set; }
}
public class Address
{
    public int    AddressID  { get; set; }
    public int    StateID    { get; set; }

    public string Street     { get; set; }
    public string City       { get; set; }
    public string PostalCode { get; set; }

    public State State                { get; set; }
    public ICollection<Person> People { get; set; }
}
public class Person
{
    public int      PersonID    { get; set; }
    public string   Name        { get; set; }

    public ICollection<Address> Addresses { get; set; }
}

My database contains tables for the above classes + the below Many:Many table:

Person.Person2Address
(
    PersonID      INT NOT NULL,
    AddressID     INT NOT NULL,
    AddressTypeID INT NOT NULL,
)

The 3 fields above are all foreign keys & the 3 together make up the Primary Key for the table.

Typically my M:M setups only involve 2 fields in the PK. And I would map it like this:

var addressCfg = mb.Entity<Address>();
addressCfg.ToTable("Address", "Geo");
addressCfg.HasMany(a => a.People)
          .WithMany(p => p.Addresses)
          .Map(mmc =>
          {
             mmc.ToTable("Person2Address", "Person");
             mmc.MapLeftKey("AddressID");
             mmc.MapRightKey("PersonID");
          });

But I don’t know how to configure this 3rd field in the PK or how CRUD would even work in EF in this case.

Any examples of how this should be handled would be greatly appreciated.

  • 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-17T09:25:07+00:00Added an answer on June 17, 2026 at 9:25 am

    You can’t map this as many-to-many relationship. You need three one-to-many relationships with an intermediate additional entity Person2Address that represents the link table with the three keys. The collections in Person and Address must both refer to this intermediate entity (and optionally also a collection in AddressType).

    The model would be like this:

    public class AddressType
    {
        public int AddressTypeID { get; set; }
        public string Name { get; set; }
    
        // public ICollection<Person2Address> Person2Addresses { get; set; }
        // optionally you can include this collection or not
    }
    
    public class Address
    {
        public int AddressID { get; set; }
        public int StateID { get; set; }
    
        public string Street { get; set; }
        public string City { get; set; }
        public string PostalCode { get; set; }
    
        public State State { get; set; }
        public ICollection<Person2Address> Person2Addresses { get; set; }
    }
    
    public class Person
    {
        public int PersonID { get; set; }
        public string Name { get; set; }
    
        public ICollection<Person2Address> Person2Addresses { get; set; }
    }
    
    public class Person2Address
    {
        public int PersonID { get; set; }
        public int AddressID { get; set; }
        public int AddressTypeID { get; set; }
    
        public Person Person { get; set; }
        public Address Address { get; set; }
        public AddressType AddressType { get; set; }
    }
    

    And the mapping with Fluent API:

    modelBuilder.Entity<Person2Address>()
        .HasKey(p2a => new { p2a.PersonID, p2a.AddressID, p2a.AddressTypeID });
    
    modelBuilder.Entity<Person2Address>()
        .HasRequired(p2a => p2a.Person)
        .WithMany(p => p.Person2Addresses)
        .HasForeignKey(p2a => p2a.PersonID);
    
    modelBuilder.Entity<Person2Address>()
        .HasRequired(p2a => p2a.Address)
        .WithMany(a => a.Person2Addresses)
        .HasForeignKey(p2a => p2a.AddressID);
    
    modelBuilder.Entity<Person2Address>()
        .HasRequired(p2a => p2a.AddressType)
        .WithMany()
        .HasForeignKey(p2a => p2a.AddressTypeID);
    

    Or use WithMany(at => at.Person2Addresses) in the last mapping if you want to include the collection in AddressType.

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

Sidebar

Related Questions

I'm using EF5 and Code-First. I have an abstract base class called FooBase. Foo1
For an application using Code First EF 5 beta I have: public class ParentObject
using Telerik.WinControls.Data; using Telerik.WinControls.UI.Export; namespace Directory { public partial class radForm : Form {
Using range() in Underscore I can make something like this: _.range(10); >> [0, 1,
I am using EF5 code-first approach and I am just wondering should I detach
I'm using EF5 code first in a simple test application at the moment to
I using manual mapping entity in my Code First on EF5 project and would
I'm using EF5 code first, and I need to wrap multiple calls to SaveChanges()
I'm started using EF5 Code First on my project and i want to create
I'm using EF5 Code First, I have entities named XXXEntity which is based on

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.