Hi,
Apologies if this has been posted elsewhere but I can't seem to find any info regarding this.
I'm trying to load an object using the LinqtoSql data context.
I have a Page and Template table. There is a relation between the two: Page.TemplateId to Template.TemplateID
The data context autocreated the Page and Template classes.
My code is:
public class PageRepository
{
Table<Page> _table = (new HospoEngineDataContext()).GetTable<Page>();
public IQueryable<Page> Pages
{
get { return _table; }
}
public Page GetPageByName(string name)
{
Page page = (from p in _table where p.Name == name select p).FirstOrDefault();
return page;
}
}
When I run GetPageByName(), it'll populate the returned Page object fine.
However it seems it also loads the Page.Template sub object too.
Is there any way I can prevent this from happening?
EDIT: I've just installed SqlServerQueryVisualizer and created a new query so I can see what sql is being generated by adding this code to the above class:
public IQueryable<Page> Test()
{
var pages = (from p in _context.Pages where p.Name == "Home Page Name" select p);
return pages;
}
It turns out that the sql being generated is:
{SELECT [t0].[PageId], [t0].[TemplateId], [t0].[Name], [t0].[Title], [t1].[TemplateId] AS [TemplateId2], [t1].[Name] AS [Name2], [t1].[TemplateFile] FROM [dbo].[Pages] AS [t0] INNER JOIN [dbo].[Templates] AS [t1] ON [t1].[TemplateId] = [t0].[TemplateId] WHERE [t0].[Name] = @p0 }
Which doesnt seem to be only loading the Pages table.