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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:58:15+00:00 2026-06-11T00:58:15+00:00

Hi guys I have a question about EF Fluent API (Code First :P). In

  • 0

Hi guys I have a question about EF Fluent API (Code First :P). In my model I have

public class TABLE_A
{
   public virtual long Id {get; set;}
   .... 
   public virtual TABLE_B MyTableBRef {get; set;}
}


public class TABLE_B
{
   public virtual long Id {get; set;}
   .... 
   public virtual TABLE_A MyTableARef {get; set;}
}

How am I supposed to map a 0..1 to 0..1 relationship?

In the database on TABLE_B I have a column (FK) that references the PK of TABLE_A.

  • 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-11T00:58:17+00:00Added an answer on June 11, 2026 at 12:58 am

    It is not possible to have navigational properties on both sides if you are using a column other than the PK of TABLE_B.

    public class TABLE_A
    {
       public virtual long Id {get; set;}
    }
    
    
    public class TABLE_B
    {
       public virtual long Id {get; set;}
       .... 
       public virutal TABLE_A MyTableARef {get; set;}
    }
    
    public class MyContext : DbContext
    {
        public DbSet<TABLE_A> As { get; set; }
        public DbSet<TABLE_B> Bs { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<TABLE_B>()
               .HasRequired(b => b.MyTableARef)
               .WithMany()
               .Map(b => b.MapKey("FK_Column_name));
        }
    }
    

    If the PK of TABLE_B is also a FK to TABLE_A then you can use shared PK mapping with navigational properties on both sides.

    Edit:

    You can use Shared PK mapping as follows

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<TABLE_B>()
           .HasRequired(b => b.MyTableARef)
           .WithOptional(a => a.MyTableBRef);
    }
    

    The Id of TABLE_B is also an FK to TABLE_A.

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

Sidebar

Related Questions

I have a question about the IEnumerator.GetEnumerator() method. public class NodeFull { public enum
I have a question about the source-code binary on Windows. #include <stdio.h> int main()
Hi guys i have a question about Internalization and the terms and condition: in
Okay guys I have a question about positioning tables within DIV's using CSS. I
Hello guys I have a specific question about the AND clause in SQL. The
Hey guys i have another question about dojo. This time it has to do
Hey guys, I have a question about asp.net I have this table with checkbox
i have a question about the imgur api. I want to create a gallery
Possible Duplicate: Is Java pass by reference? Hi guys, I have a question about
I have a question about vector push_back() .I am writing a code to acquire

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.