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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:36:39+00:00 2026-06-05T00:36:39+00:00

We have 3 entities that relate to each other as such: Foo has many

  • 0

We have 3 entities that relate to each other as such:

  • Foo has many Attachments
  • Bar has many Attachments
  • Each Attachment can belong to either of Foo or Bar.

What is the best way to model this using DbModelBuilder?

If we try:

modelBuilder.Entity<Foo>().HasMany(t => t.Attachments).WithOptional()
    .Map(m => m.MapKey("FOO_ID")).WillCascadeOnDelete(true);
modelBuilder.Entity<Bar>().HasMany(t => t.Attachments).WithOptional()
    .Map(m => m.MapKey("BAR_ID")).WillCascadeOnDelete(true);

This is not a very clean data model:
* The Attachments table will have two FK columns, one for each possible parent entities (‘FOO_ID’, ‘BAR_ID’). If we increase the number of entities that need attachments, it bloats the table.
* We need to make the backward reference optional although of course an attachment is always attached to an entity. It just needn’t necessarily be a Foo, and it needn’t be a Bar. But an attachment with all FK’s set to NULL will be allowed by the schema although it is not valid from a business view.
* In theory one Attachment could be linked to both a Foo and a Bar, which is not valid.

Instead we might map the relationships to junction tables ‘T_FOO_ATTACHMENT’ and ‘T_BAR_ATTACHMENT’:
* T_FOO_ATTACHMENT has a required foreign key to Foo (FOO_ID) and a required FK to Attachment (ATTACHMENT_ID).
* The same holds for T_BAR_ATTACHMENT

Downsides:
* An attachment could still be linked to a Foo and a Bar.
* An attachment could even be linked to multiple Foo’s, which is not valid either. Ideally we could use the fluent syntax to add DB constraints that prevent 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-05T00:36:41+00:00Added an answer on June 5, 2026 at 12:36 am

    This is not a very clean data model: The Attachments table will have
    two FK columns, one for each possible parent entities (‘FOO_ID’,
    ‘BAR_ID’).

    But it is absolutely correct from relational perspective – if you want to have relation with another table you need a new foreign key.

    But an attachment with all FK’s set to NULL will be allowed by the
    schema although it is not valid from a business view. In theory one
    Attachment could be linked to both a Foo and a Bar, which is not
    valid.

    That is your business logic. With your current model you must enforce it in your application.

    What you are looking for is some kind of named relations where the Attachment doesn’t have only FK but also the name of related table. That is something you will not achieve on relational level – it is data level (again business logic) and it cannot be mapped with EF.

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

Sidebar

Related Questions

I have 3 entities that are not related to each other and I want
I have entities Word and Tag . Both have To-Many relationship to each other,
I have two entities in Doctrine 2.1: Category and Site each category has many
Say I have two entities in Doctrine2 that are related to each other, Models\User
For each entity that has a one to many relation with other entity when
I have 2 NHibernate entities (tables) that are in a one to many relationship.
i have two Entities, Parent and Child, that are linked through a bidirectional one-to-many
I'm importing ~18,000 records of various entities that are related to each other from
I have a table AccountSecurity which is a many-to-many table that relates Account entities
I need store some review flags that relate to some entities. Each review flag

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.