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

Re: Linq deferred loading doesn't seem to work by Jon

Jon
Fri Mar 14 08:25:52 CDT 2008

Andy <andyj@med-associates.com> wrote:
> 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?

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

--
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

Re: Linq deferred loading doesn't seem to work by Chris

Chris
Fri Mar 14 08:31:04 CDT 2008

On Mar 14, 8:13 am, Andy <an...@med-associates.com> wrote:
> 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

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

Re: Linq deferred loading doesn't seem to work by Andy

Andy
Fri Mar 14 08:32:42 CDT 2008

On Mar 14, 9:25 am, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:
> Andy <an...@med-associates.com> wrote:
> > 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?
>
> When you say "Linq" - which flavour are you using? LINQ to SQL? Entity
> Framework? Some other provider?
>
> --
> 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.

Re: Linq deferred loading doesn't seem to work by Andy

Andy
Fri Mar 14 08:33:07 CDT 2008

On Mar 14, 9:31 am, Chris Dunaway <dunaw...@gmail.com> wrote:
> On Mar 14, 8:13 am, Andy <an...@med-associates.com> wrote:
>
>
>
> > 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
>
> 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

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

Re: Linq deferred loading doesn't seem to work by Jon

Jon
Fri Mar 14 09:46:06 CDT 2008

Andy <andyj@med-associates.com> wrote:
> >
> > 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

--
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

Re: Linq deferred loading doesn't seem to work by Andy

Andy
Fri Mar 14 09:56:20 CDT 2008

On Mar 14, 10:46 am, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:
> Andy <an...@med-associates.com> wrote:
> 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

Re: Linq deferred loading doesn't seem to work by Jon

Jon
Fri Mar 14 10:14:33 CDT 2008

Andy <andyj@med-associates.com> wrote:
> On Mar 14, 10:46 am, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:
> > Andy <an...@med-associates.com> wrote:
> > 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

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
> > 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.

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.

--
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

Re: Linq deferred loading doesn't seem to work by Andy

Andy
Fri Mar 14 11:56:22 CDT 2008

On Mar 14, 11:14 am, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:
> 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.

Re: Linq deferred loading doesn't seem to work by Andy

Andy
Fri Mar 14 12:53:30 CDT 2008

On Mar 14, 11:14 am, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:
> 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