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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:00:28+00:00 2026-06-13T23:00:28+00:00

Basically what I am trying to achieve is I have three tables, one parent

  • 0

Basically what I am trying to achieve is I have three tables, one parent will always have an entry in it and only one of the other two tables will be populated. The complexity that I am dealing with is that the primary key for the tables is the combination of two fields

ParentTable
-----------
UniqueID
OwnerID
[Some more fields]


ChildTable1
-----------
UniqueID
OwnerID
[Some more fields]


ChildTable2
-----------
UniqueID
OwnerID
[Some more fields]

I was wondering if anyone has any suggestions on how best to do this through EF Code First preferably using the Fluent API.

  • 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-13T23:00:29+00:00Added an answer on June 13, 2026 at 11:00 pm

    You just need to define that the primary keys are composite…

    modelBuilder.Entity<Parent>().HasKey(p => new { p.UniqueID, p.OwnerID });
    modelBuilder.Entity<Child1>().HasKey(c => new { c.UniqueID, c.OwnerID });
    modelBuilder.Entity<Child2>().HasKey(c => new { c.UniqueID, c.OwnerID });
    

    …and then define the two one-to-one relationships:

    modelBuilder.Entity<Parent>()
        .HasOptional(p => p.Child1)
        .WithRequired(); // or .WithRequired(c => c.Parent)
    
    modelBuilder.Entity<Parent>()
        .HasOptional(p => p.Child2)
        .WithRequired(); // or .WithRequired(c => c.Parent)
    

    You cannot define a constraint though (except probably by defining a trigger in the database) that would ensure that a given parent may only refer to one of the two children, but not to both. You must handle this restriction in your application’s business logic.

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

Sidebar

Related Questions

Basically what I'm trying to achieve is the following:- I have a text box
I'm trying to achieve the following layout without tables or JavaScript. It's basically the
Basically what i am trying to achieve is having the ul menu on the
What I am trying to achieve is a complex privacy routine, i have the
what I am trying to achieve is to have a persistent list of undoable
Basically what I'm trying to achieve is a program which allow users to connect
Here is what i am trying to achieve: I have a few Linux servers
I'm trying to achieve a small animated intro to a website. Basically I want
I will try and explain exactly what I want to achieve first. Imagine two
I'm using GWT and its underlaying DOM capabilities. What I'm basically trying to achieve

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.