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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:16:41+00:00 2026-05-13T11:16:41+00:00

I have a Linq to SQL class. There is a one to many relationship

  • 0

I have a Linq to SQL class.

There is a one to many relationship in my database.

The relationship maps correctly in the designer, and an EntitySet<> property is created in the designer.

When I run the code, the EntitySet<> does not populate with any data, even though there are associated records, they do not populate into the EntitySet<>

Am I missing some property or setting? Do I have to write the query myself? I feel like I’m missing something obvious.

Here is the Designer code:

[Association(Name = "Bar_Foo", Storage = "_Foo", ThisKey = "ID", OtherKey = "BarID")]
[DataMember(Order = 15, EmitDefaultValue = false)]
public EntitySet<Foo> Foos
{
    get
    {
        if ((this.serializing && (this._Foo.HasLoadedOrAssignedValues == false)))
        {
            return null;
        }
        return this._Foo;
    }
    set
    {
        this._Foo.Assign(value);
    }
}

This is the code where I am trying to access the EntitySet<>:

    partial void OnCreated()
    {
        foreach (Foo foo in Foos)
        {
            foo.DoSomething();
        }
    }

More information about my situation:

So, from above I have a class Bar with a collection of Foo. What I am trying to do is pass Bar to a UI via a WCF service. Following is my primary service call:

public class TheService : ITheService
{
    public List<Bar> GetBars()
    {
        try
        {
            using (var db = new BarDataContext())
            {
                List<Bar> Bars = new List<Bar>();

                Bars = (from B in db.Bars
                            select B).ToList();

                return Bars;
            }
        }
        catch (Exception ex)
        {
            throw new FaultException(ex.Message + " Something in GetBars() Stack Trace: " + ex.StackTrace);
        }
    }
}

Currently, when the service returns the Bars the Foos inside each Bar is null.

If I create a property in Bar that looks at Foos, I get a null reference exception.

I try to run a query in the OnCreated method to fill Foos, the ID of the current Bar is 0.

Updated Query that still doesn’t work:

                using (var ctx = new BarDataContext())
            {
                List<Bar> Bars= new List<Bar>();

                Bars= (from B in ctx.Bars
                             select B).ToList();

                foreach (Bar bar in Bars)
                {
                    bar.Foos= (from B in ctx.Bars
                               where B.ID == bar.ID
                               select B.Foos).SingleOrDefault();
                }

                return Bars;
            }

This code generates a null reference exception when I try to query Foos from Bar.

EDIT:

The code above magically stopped throwing null reference exceptions, dunno why. I find it interesting that you don’t even need to set bar.Foos in the above query, you can put the Foos query into a variable that never gets used and it will fill in the Bar.Foos property, just because you looked at Foos. Reminds me of schrodinger’s cat.

  • 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-05-13T11:16:41+00:00Added an answer on May 13, 2026 at 11:16 am

    The OnCreated event applies to a single record object, not to the entire EntitySet. It’s used primarily for setting default values for properties.

    If you wish to retrieve a result set, you need to use a query to do so. For example:

    var result = Foos.Where(a => a.ID == 7).Single();
    

    The non-lambda equivalent would be:

    var result = from a in Foos
                 where (a => a.ID == 7)
                 select a;
    

    Edit

    You can’t use the OnCreated event to fill the Foos property in Bar, for the reason stated above. As a matter of fact, you don’t have to manually fill the Foos property at all. If you have properly defined the foreign key relationship in your database and you have used the model designer to create the DataContext (*.dbml), the Foos property should already be filled when you retrieve Bar. You just need to access the Foos property from the Bar class:

    var result = from a in DataContext.Bar
                 where (a => a.ID == 7)
                 select a.Foos;
    

    This query will retrieve the collection of Foos associated with Bar #7.

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

Sidebar

Related Questions

I have a LINQ to SQL class VoucherRecord based on a simple table. One
I have a Linq-To-Sql based repository class which I have been successfully using. I
I have a BankAccount table. LINQ to SQL generates a class named BankAccount as
I have a class containing Linq To SQL objects that are used to populate
I have a class which provides generic access to LINQ to SQL entities, for
I have a bunch of entity class (generated by Linq to SQL) with a
I have a LINQ TO SQL Context that I have created that calls a
I have some entities created with LINQ-to-SQL. Six of these entities (representing values primarily
I have a linq to sql class with several table bound properties, but I
I have created small test web application which makes use of LINQ to SQL.

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.