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

  • Home
  • SEARCH
  • 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 8623823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:22:32+00:00 2026-06-12T07:22:32+00:00

I am brand new to Entity Framework code first, so any help or direction

  • 0

I am brand new to Entity Framework code first, so any help or direction would be much appreciated.

I currently have the following classes:

public partial class Customer
{
    public int Id { get; set; }
    private ICollection<Address> _addresses;
}

public partial class Address
{
    public int Id { get; set; }
    public string Street { get; set; };
    public string City { get; set; };
    public string Zip { get; set; };
}

and the following

public partial class CustomerMap : EntityTypeConfiguration<Customer>
{
    public CustomerMap()
    {
        this.ToTable("Customer");
        this.HasKey(c => c.Id);

        this.HasMany<Address>(c => c.Addresses)
            .WithMany()
            .Map(m => m.ToTable("CustomerAddresses"));
    }
}

This works as I would expect, and creates a Customer, Address and CustomerAddresses table for the mapping. Now for my question.. what would I do if I needed to modify the code to produce the following…

I want to add a CompanyCode attribute to the “CustomerAddresses” table… and then instead of constructing a collection of addresses.. i want to be able to construct a hashtable, where the key is the CompanyCode, and the value is the collection of addresses.

So if I had the following:

Customer
ID     C1

Address
ID     A1
ID     A2

CustomerAddresses
CustomerID      C1
AddressID     A1
CompanyCode    ABC

CustomerID      C1
AddressID     A2
CompanyCode    ABC

CustomerID      C1
AddressID     A2
CompanyCode    XYZ

so then, Customer.Addresses[“ABC”] would return a collection of addresses with ID, A1 and A2. Whereas Customer.Addresses[“XYZ”] would return a collection of addresses with ID A2.

Any direction/help would be much appreciated… 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-06-12T07:22:33+00:00Added an answer on June 12, 2026 at 7:22 am

    As far as I can tell it isn’t possible to introduce such a navigation property with an indexer. Your indexer is actually a query and you must express this as a query. The only way I see is that you leave the navigation collection as is and introduce a second (not mapped) property that uses the navigation collection for the filter. The big drawback is that such a filter would happen in memory with LINQ-to-Objects and requires that you always load the full collection first from the database (by eager or lazy loading for example) before you filter the collection.

    I would probably leave such a filter out of the entity itself and implement it in a repository or service class or generally the place/module where you load the entities from the database.

    The first thing you need to do is exposing the CustomerAddresses table as an entity in your model because with your additional custom property CompanyCode you can’t use a many-to-many relationship anymore, instead you need two one-to-many relationships. The new entity would look like this:

    public partial class CustomerAddress
    {
        public int CustomerId { get; set; }
        // public Customer Customer { get; set; } // optional
    
        public int AddressId { get; set; }
        public Address Address { get; set; }
    
        public string CompanyCode { get; set; }
    }
    

    And the Customer needs to be changed to:

    public partial class Customer
    {
        public int Id { get; set; }
        public ICollection<CustomerAddress> CustomerAddresses { get; set; }
    }
    

    You need to change the mapping to:

    public CustomerMap()
    {
        this.ToTable("Customer");
        this.HasKey(c => c.Id);
    
        this.HasMany(c => c.CustomerAddresses)
            .WithRequired() // or .WithRequired(ca => ca.Customer)
            .HasForeignKey(ca => ca.CustomerId);
    }
    

    And create a new mapping for the new entity:

    public CustomerAddressMap()
    {
        this.ToTable("CustomerAddresses");
        this.HasKey(ca => new { ca.CustomerId, ca.AddressId, ca.CompanyCode });
        // or what is the PK on that table?
        // Maybe you need an Id property if this key isn't unique
    
        this.HasRequired(ca => ca.Address)
            .WithMany()
            .HasForeignKey(ca => ca.AddressId);
    }
    

    Now, in some service class you could load the filtered addresses:

    public List<Address> GetAddresses(int customerId, string companyCode)
    {
        return context.CustomerAddresses.Where(ca =>
            ca.CustomerId == customerId && ca.CompanyCode == companyCode)
            .ToList();
    }
    

    Or, if you want to load the customer together with the filtered addresses:

    public Customer GetCustomer(int customerId, string companyCode)
    {
        var customer = context.Customer.SingleOrDefault(c => c.Id == customerId);
        if (customer != null)
            context.Entry(customer).Collection(c => c.CustomerAddresses).Query()
                .Where(ca => ca.CompanyCode == companyCode)
                .Load();
        return customer;
    }
    

    The last example are two database queries.

    In the Customer entity you could use a helper property that projects the addresses out of the CustomerAddresses collection:

    public partial class Customer
    {
        public int Id { get; set; }
        public ICollection<CustomerAddress> CustomerAddresses { get; set; }
    
        public IEnumerable<Address> Addresses
        {
            get
            {
                if (CustomerAddresses != null)
                    return CustomerAddresses.Select(ca => ca.Address);
                return null;
            }
        }
    }
    

    Keep in mind that this property does not query the database and the result relies on what is already loaded into CustomerAddresses.

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

Sidebar

Related Questions

I just downloaded and installed the brand new Entity Framework 5.0 beta 2 which
I have the 2 following entities: @Entity @Table(name = brand) public class Brand {
I'm brand new to the Entity Framework and trying to learn all it can
Brand new to the Log4Net library and there is something I have not been
Brand new to android and facing a weird problem.Check out the code below FirstActivity.java:
The brand-new WCF-based code needs (in the meanwhile) to provide a service to a
I'm new to EF, and I've just started a new project. I currently have
I have a brand new e-commerce website ( http://missfrisette.com/showphotos.php ) each item (hair clip)
I am brand new to Java :) I have 2 String lists and I
I'm brand new to MVC and have just come across a scenario that I

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.