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

The Archive Base Latest Questions

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

This should be a simple question for the well versed EF user. I have

  • 0

This should be a simple question for the well versed EF user.

I have the following schema (in my head) of how the relationships between the tables should look.

[FooBar]      [Foo]          [Bar]

FooId PK,FK   Id PK          Id PK
BarId PK,FK   BarId FK       Name
IsRead        Name           Description
              Description    

Though, when I try to generate the schema using EF code-first it fails to interpret the relationships between the entities as I’ve interpreted them (adds foreign key FooId to the [bar] table) and fails to fully create the [FooBar] bridge table.

If someone could guide me on how to achieve the above schema using EF4 code-first I’d appreciate it. Whether the solution involves attributes on my POCO models, fluent configurations or a hybrid of both doesn’t matter much – as long as the desired database schema is created.


POCO Models:

public class Foo
{
    public int Id { get; set; }
    public string Text { get; set; }
    public string Description { get; set; }
    public int BarId { get; set; }

    public Bar Bar { get; set; } /* bar entity */

    public virtual ICollection<Bar> BridgedBars { get; set; }

    public Foo()
    {
        Bars = new List<Bar>();
    }
}

public class Bar
{
    public int Id { get; set; }
    public string Text { get; set; }
    public string Description { get; set; }

    public virtual ICollection<Foo> Foos { get; set; }
    public virtual ICollection<Foo> BridgedFoos { get; set; }

    public Bar()
    {
        Foos = new List<Foo>();
        BridgedFoos = new List<Foo>();
    }
}

public class FooBar
{
    public int FooId { get; set; }
    public int BarId { get; set; }

    public virtual Foo Foo { get; set; }
    public virtual Bar Bar { get; set; }

    public bool IsRead { get; set; }
}
  • 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-05T03:12:40+00:00Added an answer on June 5, 2026 at 3:12 am

    Your model will indeed create a foreign key FooId in the Bar which belongs to the relationship defined by Foo.BrideBars. EF doesn’t relate this navigation property to one of the ICollection<Foo> properties in Bar because there are two of them and EF cannot determine uniquely which is the correct pair. As a result it creates a relationship for Foo.BrideBars without a navigation property on the other end. So to speak, there is an invisible Bar.Foo property which causes the foreign key.

    The database schema you want to map to a model does not really represent a many-to-many relationship but instead two one-to-many relationships with the intermediate “bridge” entity FooBar. You must use this class in the navigation properties to define the correct relationships. It would look like this:

    public class Foo
    {
        public int Id { get; set; }
        public string Text { get; set; }
        public string Description { get; set; }
    
        public int BarId { get; set; }
        public Bar Bar { get; set; }
    
        public virtual ICollection<FooBar> FooBars { get; set; }
    }
    
    public class Bar
    {
        public int Id { get; set; }
        public string Text { get; set; }
        public string Description { get; set; }
    
        public virtual ICollection<Foo> Foos { get; set; }
        public virtual ICollection<FooBar> FooBars { get; set; }
    
    }
    
    public class FooBar
    {
        [Key, Column(Order = 0)]
        public int FooId { get; set; }
        [Key, Column(Order = 1)]
        public int BarId { get; set; }
    
        public virtual Foo Foo { get; set; }
        public virtual Bar Bar { get; set; }
    
        public bool IsRead { get; set; }
    }
    

    The correct relationships will be detected by naming conventions in this model. Only for the FooBar entity it is necessary to define a key explicitly because the property names do not meet the conventions (no Id and no FooBarId property). In this model it makes sense to use a composite key in FooBar.

    I guess, your real classes and properties don’t have the name Foo and Bar. If your real names do not follow the conventions you possibly have to specify the relationships with annotations – or with Fluent API:

    modelBuilder.Entity<Foo>()
        .HasRequired(f => f.Bar)
        .WithMany(b => b.Foos)
        .HasForeignKey(f => f.BarId);
    
    modelBuilder.Entity<FooBar>()
        .HasKey(fb => new { fb.FooId, fb.BarId }); // replaces the [Key] annotations
    
    modelBuilder.Entity<FooBar>()
        .HasRequired(fb => fb.Foo)
        .WithMany(f => f.FooBars)
        .HasForeignKey(fb => fb.FooId);
    
    modelBuilder.Entity<FooBar>()
        .HasRequired(fb => fb.Bar)
        .WithMany(b => b.FooBars)
        .HasForeignKey(fb => fb.BarId);
    

    In your database schema the FooBar table will have a composite primary key:

    [FooBar]       [Foo]          [Bar]
    
    FooId PK,FK    Id PK          Id PK
    BarId PK,FK    BarId FK       Name
    IsRead         Name           Description
                   Description    
    

    But having a PK in FooBar is necessary because every entity in an EF model must have a key property defined – either single or composite – which maps to a primary key in the database table.

    In this question – Create code first, many to many, with additional fields in association table – are more details how to work with such a type of relationship. (Sometimes people also call it “many-to-many relationship with payload” (the IsRead property is the “payload” in your example model), but in fact it’s not many-to-many.)

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

Sidebar

Related Questions

This should be a simple question, but I just can't recall the relevant API.
This should be a simple question, but I can't get it to work :(
This should be a simple question, but I just can't seem to figure it
This should be very simple question. There are many programming languages out there, compiled
Okay, so this should be a simple question, but I'm fairly new at programming,
This is (should be) a simple question. I'd like to create a component like
I think this should be a fairly simple question for anyone who knows PHP.
This should be simple - can't figure out where I'm going wrong: We have
I have, what should be, a simple question on drag'n'drop. I have a fresh
TLDR; this is not a well-phrased question, so you should probably not bother with

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.