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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:15:49+00:00 2026-06-18T15:15:49+00:00

I have the following models defined: public class Account { public int Id {get;

  • 0

I have the following models defined:

public class Account {
   public int Id {get; set;}
   ... // other account properties

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

public class Address {
    public int Id {get; set;}
    public string Street1 {get; set;}
    public string Street2 {get; set;}
    ... // other standard address properties
}

public class AccountAddress : Address {
  public int AccountId {get; set;}
  public DateTime? BeginDate {get; set;}
  public string Notes {get; set;}
  ... // other account address specific properties
}

public class Site {
  public int Id {get; set;}
  public Address SiteAddress {get; set;}
}

The idea is that any Address can be tied to any Account as well as to any Site. However, any time an Address is associated with an Account, there may be additional information that needs to be stored about that address.

Since AccountAddress inherits from Address, I wanted to use TPT (Table Per Type) inheritance to map Address properties to the Address table and the extended properties to an table of extended address properties.

I tried this:

public class AccountConfiguration : EntityTypeConfiguration<Account>
{
    public AccountConfiguration()
    {
        ToTable("Accounts");
        HasKey(a => a.Id);
        HasMany(a => a.Addresses).WithMany().Map(x =>
        {
           x.MapLeftKey("accountId");
           x.MapRightKey("addressId");
           x.ToTable("accountaddresses");
        }
     }
 }

 public class AddressConfiguration : EntityTypeConfiguration<Address>
 {
     public AddressConfiguration()
     {
         ToTable("addresses");
         HasKey(a => a.Id);
         ... // other property mappings
     }
 }

 public class AccountAddressConfiguration : EntityTypeConfiguration<AccountAddress>
 {
     public AccountAddressConfiguration() 
     {
        ToTable("addressextended");
        ... //other property mappings
     }
  }

The problem here is that there is nothing defined to map accountId into the addressextended table. So if an Address with an Id of 40 is tied to two different accounts, doing the following query:

var _account = _context.Accounts
   .Include(a => a.Addresses)
   .SingleOrDefault(a => a.Id = 1234);

sometimes gives the extended properties for the wrong account.

What do I need to do in order to get this working?

EDIT:
While the accepted answer did not specifically answer my question (i.e. how can I define a many to many relationship between two entities where one entity is defined with TPT inheritance mapping), it did provide some help in coming up with an acceptable work around.

I did drop the entity inheritance and simply added address as a property of my AccountAddress entity. I then gave AccountAddress (addressextended table) its own primary id. I changed the accountaddresses table to a join table that contains the account id and the accountaddress id (the new id of the addressextended table).

I also had to add a navigation property to AccountAddress for Address and a mapping to go along with that.

  • 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-18T15:15:50+00:00Added an answer on June 18, 2026 at 3:15 pm

    I believe you may be able to work around this problem by changing the way you view the “AccountAddress” and “SiteAddress” tables. Instead of them being subclasses of Address, why not make them “join tables”. The AccountAddress entity joins an address and an account, so technically it has to own an account as well as an address. You can also specify other information that are relevant when linking an address with an account (e.g. address type, purge dates, etc).

    From what I understand, the goal you are trying to achieve is to link many addresses to many accounts (or sites), and keep some metadata regarding each link.

    public class Account {
       public int Id {get; set;}
       ... // other account properties
    
       public ICollection<AccountAddress> Addresses {get; set;}
    }
    
    public class Address {
        public int Id {get; set;}
        public string Street1 {get; set;}
        public string Street2 {get; set;}
    
        public ICollection<AccountAddresses> Accounts {get; set;} // this denotes your two-way connection with the Account entity
        ... // other standard address properties
    }
    
    public class AccountAddress {
      public int AccountId {get; set;}
      public virtual Account {get; set;} // these are your "Account" properties
    
      public int AddressId {get; set;}
      public virtual Address {get;set;} //these are your "Address" properties
    
      public DateTime? BeginDate {get; set;}
      public string Notes {get; set;}
      ... // other account address specific properties
    }
    

    This model would now denote the following relationship:

    1 Account <-> * AccountAddress * <-> 1 Address

    This simulates your Many to Many relationship between addresses and accounts by going through the AccountAddress entity.

    You should then configure both the Account and Address entities to demonstrate their One-to-Many relationship with something like:

    modelBuilder.Entity<Account>().HasMany(a => a.AccountAddresses)
                                  .WithRequired(aa => aa.Account)
                                  .HasForeignKey(aa => aa.AccountId);
    
    modelBuilder.Entity<Address>().HasMany(a => a.Accounts)
                                  .WithRequired(ac => ac.Address)
                                  .HasForeignKey(ac => ac.AddressId);
    

    Your can modify your query to look something like:

    var _account = _context.Accounts
       .Include(a => a.Addresses)
       .Include(a => a.Addresses.Address)
       .SingleOrDefault(a => a.Id = 1234);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models: WeatherForecast: namespace TravelAssistant.Model.HelperModels.Weather public class WeatherForecast:EntityBase<int> { public IList<DayForecast>
I have the following view models: public class Search { public int Id {
All, i have the following model defined, class header(models.Model): title = models.CharField(max_length = 255)
I have the following Models and ViewModels (edited for brevity): public class Advert {
I have the following collection contract defined: [CollectionDataContract(Name = Centres)] public class Centres :
I have defined a base class with some properties like: public string CreatedByName {
I have a Domain model that countains the following properties : public class Profile
I have defined the following Model public class TodayOrder { public string ID_ORIG {
I have two classes defined as such: public class Questionnaire { public int QuestionnaireID
I have the following model defined in .NET: Public Class Request <Key()> Public Property

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.