Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Linq deferred loading doesn't seem to work

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andy - 14 Mar 2008 14:13 GMT
Hi,

I have an Association on my table; the association doesn't seem to
load.  Here's the association:

private EntityRef<Shared.Employee> createdBy =
default( EntityRef<Shared.Employee> );

[Association( ThisKey = "CreatedById", IsForeignKey = true )]
public Shared.Employee CreatedBy {
  get { return createdBy.Entity; }
  set { createdBy.Entity = value; }
}

I try to access the association like this:

createdByName = myClass.CreatedBy.LogonName; // Shouldn't this cause
CreatedBy to load?

Instead I get a NullReferenceException.  CreatedById is a valid value
and a record does exist, but Linq never seems to even bother executing
the select to load the Shared.Employee instance.

Any ideas?

Thanks
Andy
Jon Skeet [C# MVP] - 14 Mar 2008 14:25 GMT
> I have an Association on my table; the association doesn't seem to
> load.  Here's the association:
[quoted text clipped - 18 lines]
>
> Any ideas?

When you say "Linq" - which flavour are you using? LINQ to SQL? Entity
Framework? Some other provider?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Andy - 14 Mar 2008 14:32 GMT
> > I have an Association on my table; the association doesn't seem to
> > load.  Here's the association:
[quoted text clipped - 25 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

Oh sorry, I'm using Linq to Sql.
Jon Skeet [C# MVP] - 14 Mar 2008 15:46 GMT
> > When you say "Linq" - which flavour are you using? LINQ to SQL? Entity
> > Framework? Some other provider?
>
> Oh sorry, I'm using Linq to Sql.

And was the code you've shown for the association the code created by
the LINQ to SQL designer, or was it hand-written?

The model I've got in my test app has a lot more code for the setter...
however, it all works. For example, in my test defect database, I've
got:

           using (var context = new DefectModelDataContext())
           {
               context.Log = Console.Out;

               Console.WriteLine("Loading defect");
               var defect = (from bug in context.Defects
                            where bug.Summary.Contains("Welsh")
                            select bug).Single();

               Console.WriteLine("Using Project property...");
               Console.WriteLine("Project={0}", defect.Project.Name);
           }

The output is:

Loading defect
SELECT [t0].[DefectID] AS [ID], [t0].[Created], [t0].[LastModified],
[t0].[Summary], [t0].[Severity], [t0].[Status], [t0].
[AssignedToUserID], [t0].[CreatedByUserID], [t0].[ProjectID]
FROM [dbo].[Defect] AS [t0]
WHERE [t0].[Summary] LIKE @p0
-- @p0: Input NVarChar (Size = 7; Prec = 0; Scale = 0) [%Welsh%]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build:
3.5.21022.8

Using Project property...
SELECT [t0].[ProjectID], [t0].[Name]
FROM [dbo].[Project] AS [t0]
WHERE [t0].[ProjectID] = @p0
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build:
3.5.21022.8

Project=Skeety Media Player

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Andy - 14 Mar 2008 15:56 GMT
> And was the code you've shown for the association the code created by
> the LINQ to SQL designer, or was it hand-written?

It was created from the designer, then modified.  See, I have my own
custom DAL that also used types for tables and attributes for column
information.  Much simpler than linq, but less powerful.  So I'm
transitioning by adding Linq attributes to these same classes, and
copying code from another temporary project where I create the classes
using the designer.  The code I posted is all that the designer
generates.

I don't know if this is part of it or not.. but I do all select
statements through a view, not allowing direct access to the tables,
and all updates are done via stored procedures

> The model I've got in my test app has a lot more code for the setter...
> however, it all works. For example, in my test defect database, I've
> got:

What addition code is in your setter?  Thanks for the quick feedback..
I'm leaving for a few hours, but will be checking back today because
I'm stuck on this problem.

Thanks
Andy
Jon Skeet [C# MVP] - 14 Mar 2008 16:14 GMT
> > And was the code you've shown for the association the code created by
> > the LINQ to SQL designer, or was it hand-written?
[quoted text clipped - 10 lines]
> statements through a view, not allowing direct access to the tables,
> and all updates are done via stored procedures

Okay, it may well have something to do with that - I don't know.

> > The model I've got in my test app has a lot more code for the setter...
> > however, it all works. For example, in my test defect database, I've
[quoted text clipped - 3 lines]
> I'm leaving for a few hours, but will be checking back today because
> I'm stuck on this problem.

The setter is quite big - but I wouldn't expect the setter to actually
be called for the entity itself. Do you have a property for the foreign
key, and does that get called?

I would suggest creating a new project and *just* using the LINQ to SQL
designer, and see if that works - then compare the two.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Andy - 14 Mar 2008 17:56 GMT
> The setter is quite big - but I wouldn't expect the setter to actually
> be called for the entity itself. Do you have a property for the foreign
> key, and does that get called?

I wouldn't expect it to matter either.  I do have a property for the
FK:

[Column, DataField]
public Int32? CreatedById { get; set; }

> I would suggest creating a new project and *just* using the LINQ to SQL
> designer, and see if that works - then compare the two.

Ok, I'll give that a shot.
Andy - 14 Mar 2008 18:53 GMT
> I would suggest creating a new project and *just* using the LINQ to SQL
> designer, and see if that works - then compare the two.

Ok, I did this, and apparently you MUST specify Storage in the
Association; I guess that's required so that Linq can bypass the
property setter or something.  It would be nice if this didn't
silently fail though..

Thanks for your help.

Andy
Chris Dunaway - 14 Mar 2008 14:31 GMT
> Hi,
>
[quoted text clipped - 24 lines]
> Thanks
> Andy

Does the other class have it's primary key identified with the
IsPrimaryKey property of the Column attribute?  If not, you might need
to add the OtherKey property to your Association attribute.

Chris
Andy - 14 Mar 2008 14:33 GMT
> > Hi,
>
[quoted text clipped - 30 lines]
>
> Chris

Yes, the Shared.Employee class has a Column with IsPrimaryKey = true.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.