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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:13:41+00:00 2026-06-09T03:13:41+00:00

I’m a gibbering wreck trying to get EF code first to let me do

  • 0

I’m a gibbering wreck trying to get EF code first to let me do something that I could do in 2 minutes in SQL. Had I not already spent 5 days trying to get it to work, I’d just code up my database in DDL and use ADO.NET. But I digress…

I want to have 2 tables, where each record in A has a corresponding record in B. They’re both part of the same object; they need to be in separate tables for reasons I won’t go into (but they really do, so don’t go there). If I were designing it from the database end, I’d simply have an FK relationship from B to A. Job done.

In EF Code First, I’ve tried using both the shared primary key method and the one-to-one foreign key association method, and neither work for me. I’ve also tried 100 or so combinations of all the variants I can think of, and I’m no further forward.

As I said, all I want is there to be a navigable relationship from A to B (and back would be nice, but I’ve read that’s not possible), and for that relationship to be lazy-loaded, so that I can say a.b and have access to the fields of b.

I can’t possibly enumerate all the things I’ve tried, so let me just give an example of what nearly works:

class Foo
{
    public int Id { get; set; }
    public string FooProperty { get; set; }

    public virtual Bar Bar { get; set; }
}

class Bar
{
    public int Id { get; set; }
    public string BarProperty { get; set; }
}

Note that there’s no back-reference from Bar to Foo, since (a) SQL Server would complain about multiple cascade delete paths, and (b) EF would complain about not knowing which side is the principal end of the association. So… fine – I can live without it.

What this gets me in the database is a Foos table with Id, FooProperty and Bar_Id fields, and a Bars table with Id and BarProperty fields. That’s pretty close to they way I’d model it in SQL, although I’d probably put the FK field in Bar rather than Foo. But since it’s 1:1 it doesn’t really matter, I guess.

The reason I say that this nearly works is that if I add a Bar and associated Foo and then load them back in, the Bar property of the Foo object is null.

using (var dbContext = new MyDbContext())
{
    var foo = dbContext.Foos.Create();
    foo.FooProperty = "Hello";
    dbContext.Foos.Add(foo);

    var bar = dbContext.Bars.Create();
    bar.BarProperty = "world";

    foo.Bar = bar;

    dbContext.SaveChanges();
}

using (var dbContext = new MyDbContext())
{
    foreach (var foo in dbContext.Foos)
        Console.WriteLine(foo.Bar.Id); // BOOM! foo.Bar is null
}

I would normally expect the evaluation of foo.Bar to trigger lazy-loading of the Bar object, but it doesn’t – that property remains null.

How can I fix it?

  • 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-09T03:13:42+00:00Added an answer on June 9, 2026 at 3:13 am

    Thsi should work:

    Context

    public class FoobarCtx : DbContext
    {
        public DbSet<Bar> Bars { get; set; }
        public DbSet<Foo> Foos { get; set; }
    
        public FoobarCtx()
        {
    
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Bar>()
                .HasRequired(f => f.Foo)
                .WithRequiredDependent(b => b.Bar)
                .Map(x => x.MapKey("FooId"))
                .WillCascadeOnDelete(true);
        }
    }
    

    Entities

    public class Foo
    {
        public int Id { get; set; }
    
        public string Foo1 { get; set; }
        public string Foo2 { get; set; }
    
        public virtual Bar Bar { get; set; }
    }
    
    public class Bar
    {
        public int Id { get; set; }
        public string Bar1 { get; set; }
        public string Bar2 { get; set; }
    
        public virtual Foo Foo { get; set; }
    }
    

    I tested it in EF 4.3, but I think it should work also in EF5. The Key is the OnModelCreating method. There you can define either one or the other as the principal/descendant and get rid of the Microsoft SQL restriction.

    For more information see this blog-post. For more information about the model builder (fluent API), go here.

    To enable lazy loading, use the DbContext.FooSet.Create() method. Example here.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.