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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:36:44+00:00 2026-06-10T18:36:44+00:00

I having some issues setting up the correct relationships that I require for a

  • 0

I having some issues setting up the correct relationships that I require for a database.

In words…
I want to have a Firm which can multiple (zero:many) Branch

On the Firm I want to be able specify a HeadOffice which will exist will exist in the Branch table (zero:one)

What I expected to see is the Firm table to have the following fields: Id, Name, HeadOfficeId and on the Branches I expect to see: Id, TradingAs, Name, Firm_Id

Instead I see:

Firm table to have the following fields: Id, Name, HeadOfficeId

Branches I expect to see: Id, TradingAs, Name, Firm_Id, Firm_Id1

The model classes are as shown

public class Firm
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Branch> FirmBranches { get; set; }

    public virtual LawFirmBranch HeadOffice { get; set; }
}

public class Branch
{
    public int Id { get; set; }
    public string TradingAs {get;set;}
    public string Name {get;set;}


    public Firm Firm { get; set; }

}

I understand that this causes a circular reference type issue and I am happy to have the Firm HeadOffice to initially to be Null until there are values in the FirmBranches property.

Is there some way that I can specify that the HeadOffice has the null or one type relationship

  • 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-10T18:36:46+00:00Added an answer on June 10, 2026 at 6:36 pm

    If you’re wanting to explicitly do this in your model, you can do this via data annotations:

    public class Firm
    {
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Branch> FirmBranches { get; set; }
    
    [ForeignKey("HeadOffice")]
    public int? FirmId {get;set;}
    
    public virtual LawFirmBranch HeadOffice { get; set; }
    

    }

    If you dont want this exposed in your code, you’ll need to do this via fluent:

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Firm>()
                .HasOptional(f => f.HeadOffice)
                .WithOptionalDependent();
        }
    

    This creates:

    enter image description here

    If you want to change the name of the key, you’d add a mapping to the end of the fluent map:

            modelBuilder.Entity<Firm>()
                .HasOptional(f => f.HeadOffice)
                .WithOptionalDependent()
                .Map(m => m.MapKey("YourKeyName"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some issues setting up Reporting Server. I can open http://localhost/reports only
I am having some issues with setting values using MacRuby and Cocoa. I have
I'm having some issues setting up django-mssql on Win Server 2008 R2. I have
I've been having some issues setting up a list of items and moving them
I'm having some issues to deserialize a Json array that follows this format: [
I am having some issues with a code that is occasionally and sporadically throwing
Recently I was having some issues with a singelton class that was lazy initializing
I am having some serious pathing issues with a website that I am creating.
I'm having some issues setting a generic list property of a POCO object when
I'm having some issues in RoR with some model methods I am setting. I'm

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.