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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:13:18+00:00 2026-06-13T04:13:18+00:00

I am building a domain model that requires several tables to be able to

  • 0

I am building a domain model that requires several tables to be able to be references by more than one possible parent table. Something like you might have a table to store notes or files and those notes and/or files can be associated with different parent entities. Not that the same “file” or “note” can be associate with multiple owners, but that of 10 rows in a “files” table, 3 of them might be owned by rows from a “Customer” table, 3 of them might be owned by rows from an “Orders” table, and 4 of them might be owned by rows from a “Person” table.

The owning tables all have virtual ICollections of the child tables.

When I ran my model through the code first migration for the initial migration, and looked at the generated fluent code, the table definition for these tables included multiple id columns that referred back to the source table, so that there would be a column “customer_id,” “person_id,” etc. (one for each possible “owning entity.” This doesn’t seem quite “right.” Is there a better way to model this in EF with CodeFirst? Like being able to use a discriminator column in the child table that provides a “hint” about the owning entity?

Thanks,
-MrB

  • 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-13T04:13:19+00:00Added an answer on June 13, 2026 at 4:13 am

    If you create a many-to-many relationship between notes and all entities that can have notes, you will have a junction table for each owning type and a nice and clean note table. The simplest way to create the many-to-many relationships is to override the DbContext.OnModelCreating method.

    Your DbContext:

      public class MyDatabase : DbContext
      {
        // Protected methods
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
          base.OnModelCreating(modelBuilder);
    
          modelBuilder.Entity<Customer>().HasMany(x => x.Notes).WithMany();
          modelBuilder.Entity<Order>().HasMany(x => x.Notes).WithMany();
        }
    
        public MyDatabase() : base("MyDatabase")
        {
        }
    
        // Properties
    
        public DbSet<Customer> Customers { get; set; }
    
        public DbSet<Order> Orders { get; set; }
    
        public DbSet<Note> Notes { get; set; }
      }
    

    This will produce the following tables: Customer, Orders, Notes, CustomerNotes and OrderNotes with the last two being junction tables with only two columns each.

    NOTE! With this solution it is also possible for entities to share notes, e.g. a Customer and an Order can own the same Note.

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

Sidebar

Related Questions

So no doubt that building a domain model is something that I think happens
I'm building an application with a domain model using CQRS and domain events concepts
I'm building a WPF application and while creating my domain model I used ObservableCollection<T>
I am building out my domain model and continuing to refactor it. As I
I'm building an ASP.NET MVC application that uses a DDD (Domain Driven Design) approach
We're finally building a domain model. The domain model includes interfaces for loosely coupling
I have been building a new application using my current understanding of domain driven
building a site using PHP and MySQL that needs to store a lot of
Building a website that has English & Japanese speaking users, with the Japanese users
I'm building ASP.Net MVC2 application on top of a legacy database. Every table in

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.