Linq deferred loading doesn't seem to work

  • Thread starter Thread starter Andy
  • Start date Start date
A

Andy

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
 
Andy said:
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?
 
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
 
Andy said:
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?

Oh sorry, I'm using Linq to Sql.
 
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.
 
Andy said:
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
 
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
 
Andy said:
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.
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.
 
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.
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top