tags:

views:

23

answers:

1

Hi, this should be pretty simple.

I have LINQ to SQL (C# 3.5 / SQL Server) working well, with a simple relationship in place between two tables Conferences and ConferenceAttendees.

I have already created and committed a Conference, then I add 3 ConferenceAttendees using the following code (repeated 3 times):

ConferenceAttendee NewAttendee = new ConferenceAttendee();
NewAttendee.ConferenceId = ConferenceID;
NewAttendee.Name = ContactName;
NewAttendee.Email = ContactEmail;
db.ConferenceAttendees.InsertOnSubmit(NewAttendee);
db.SubmitChanges();

Works great and I see the 3 new attendees appear in the database.

UPDATE: Then, using a brand new datacontext, I try the following:

string Names = String.Empty;
Conference RequestedConference = db.Conferences.FirstOrDefault(x => x.ConferenceId == ConferenceID);
   foreach (ConferenceAttendee Attendee in RequestedConference.ConferenceAttendees)
      Names += Attendee.Name + ", ";

But it doesn't have any related Attendees attached to it! (They definitely exist in the database and have been committed). But RequestedConference.ConferenceAttendees always has a count of 0 so the loop is never entered..

foreach (ConferenceAttendee Attendee in this.ConferenceAttendees)
{ Names += Attendee.Name; }

I'm doing this inside a Partial Class Conference, with a method I've made called PrintAllAttendeeNames().

What am I doing wrong and why doesn't this fresh datacontext easily see these related objects via LINQ when they ARE in the database committed already?

(Note: I've already tried calling

db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues);

to no avail..)

Many thanks

CONFESSION UPDATE

I am an absolute moron. I had the dataContext setup as static. I never normally do that, that's why I never normally have problems with lazy loading.

I had this:

public static MyDataContext db = new MyDataContext(ConfigurationManager.ConnectionStrings["MyDBConnection"].ConnectionString);

Of course, changing it to this solved the problem!!!

public MyDataContext db = new MyDataContext(ConfigurationManager.ConnectionStrings["MyDBConnection"].ConnectionString);
+1  A: 

A couple things. First, you should only submit your changes when you have made all the changes. You don't need to call SubmitChanges every time you create a new attendee. Create all three first, InsertOnSubmit each one, then make a single call to SubmitChanges. LINQ to SQL will generate more efficient, chunkier calls that way.

Second, I'm a little confused about the attendee loading issue. They should lazy load on the first access of the Attendee property. Each individual Conference would incur a separate query when its property is first accessed, so this isn't the most efficient way to handle it. You can tell L2S to pre-load all attendees for each Conference object that you load. You would do that with the DataLoadOptions class:

using (var context = new SomeContext(...))
{
    var options = new DataLoadOptions();
    options.LoadWith<Conference>(c => c.ConferenceAttendees);

    context.LoadOptions = options;

    var conferencesWithAttendees = from c in context.Conferences
                                   where c.Year = DateTime.Now.Year
                                   select c;

    foreach (var conferenceWithAttendee in conferencesWithAttendees)
    {
        conferenceWithAttendee.PrintAllAttendeeNames();
    }
}
jrista
Thanks for the first point, good to know and I will do from now on. I am still having problems. Yes, lazy loading normally works fine for me too. It must be something really simple! See above: I have updated to explain the code I am using to load in the Conference and iterate through the Attendees. But the Count is always 0!
Aaron
I am a moron. I have declared the dataContext as static!!! GRRR!!!Many thanks for your help and sorry to waste your time. 1 point.
Aaron
Generally speaking, a DataContext should NEVER stick around for longer than the time it takes to do some work. The DataContext is a unit of work class, so making it a field or a property of a class is a bad idea. Create as late as possible, dispose of as early as possible, and create a new instance every time you perform work in a unit.
jrista