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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:25:52+00:00 2026-06-09T04:25:52+00:00

I want to store some objects where there is exactly one Bar for each

  • 0

I want to store some objects where there is exactly one Bar for each Foo.

I have some POCO objects that look something like this:

public class Foo
{
    public int Id { get; set; }
    public string FooProperty { get; set; }
    public int BarId { get; set; }
    public virtual Bar Bar { get; set; }
}
public class Bar
{
    public int Id { get; set; }
    public string BarProperty { get; set; }
    public int FooId { get; set; }
    public virtual Foo Foo { get; set; }
}

The Foo and Bar have a 1:1 relationship, and based on the reading I’ve done I tried the following in my DbContext class:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();

    modelBuilder.Entity<Foo>()
                   .HasRequired(x => x.Bar)
                   .WithRequiredPrincipal(x => x.Foo);

    base.OnModelCreating(modelBuilder);
}

The backing store is SQL Server, and this does indeed create me tables with a 1:1 relationship. However, the FK relationship from Bar to Foo is on the Id field of both tables, whereas I would expect it to be from the FooId field of the Bar table to the Id field of the Foo table.

It seems that EF has decided to keep the PK (Id) field of both tables in sync, and is essentially ignoring my BarId/FooId columns.

What am I doing wrong?

  • 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-09T04:25:53+00:00Added an answer on June 9, 2026 at 4:25 am

    Are you sure you want a 1:1 relationship? If for every foo there is a single bar, and every bar there is a single foo, EF will use the primary keys for the relationship, and it probably should. Are you sure you don’t want a 1:many or 1:0..1 relationship?

    If you want a Foo to be able to have many bars, so you can define a FK you could change your fluent:

      modelBuilder.Entity<Foo>()
                    .HasRequired(x => x.Bar)
                    .WithMany()
                    .HasForeignKey(f => f.BarId);
    

    Here’s a blog post about One-To-One foreign key relationships which might help

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

Sidebar

Related Questions

I have some files that contain logs of objects. Each file can store objects
Want to know ways to store OBJECTS of a Class in some persistent storage.
Is this possible? Situation is that I want to store some XML in my
I want to temporary store some mapping data. The mapping is one to one.
I store some data in MySQL and want to filter out rows that match
I have an NSOperation which fetches some objects from a core data persistent store
I want to store some objects and then be able to retrieve them later
I have a general (custom) jQuery plugin. $.fn.something(). This gets called on some objects
Suppose I have some objects, and I want the user to be able to
I want to use session object in my web app.I want to store some

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.