The database will be called in this line:
this.ApplicationRole = user.ApplicationRole;
Then, your object will be fetched and will not be lazy loaded any more.
To make sure that this will happen, you have to do some configuring:
lazy loading and proxy creation must be enabled:
public Context()
: base("YourConnectionString")
{
Configuration.LazyLoadingEnabled = true;
Configuration.ProxyCreationEnabled = true;
}
Navigation properties must be declared as virutal:
public class ApplicationUser
{
//...
public virtual ApplicationRole ApplicationRole { get; set; }
//...
}
EXAMPLE IN ACTION
Supposed that you have these classes
public class ApplicationUser
{
public int ApplicationUserId { get; set; }
public string FirstName { get; set; }
public virtual ApplicationRole ApplicationRole { get; set; }
public static Context db { get; set; }
public ApplicationUser()
{
}
public ApplicationUser(int userId)
{
ApplicationUser user = ApplicationUser.Select(userId);
this.FirstName = user.FirstName;
this.ApplicationRole = user.ApplicationRole; //database will be hit here
}
public static ApplicationUser Select(int userId)
{
//this uses EF to return an Application User object
return db.Users.Find(userId);
}
}
public class ApplicationRole
{
public int ApplicationRoleId { get; set;}
public string Name { get; set; }
}
First Hit:
return db.Users.Find(userId);
Generated SQL:
SELECT
[Limit1].[ApplicationUserId] AS [ApplicationUserId],
[Limit1].[FirstName] AS [FirstName],
[Limit1].[ApplicationRole_ApplicationRoleId] AS [ApplicationRole_Application
RoleId]
FROM ( SELECT TOP (2)
[Extent1].[ApplicationUserId] AS [ApplicationUserId],
[Extent1].[FirstName] AS [FirstName],
[Extent1].[ApplicationRole_ApplicationRoleId] AS [ApplicationRole_Applic
ationRoleId]
FROM [dbo].[ApplicationUsers] AS [Extent1]
WHERE [Extent1].[ApplicationUserId] = @p0
) AS [Limit1]
-- p0: '1' (Type = Int32)
Second Hit
this.ApplicationRole = user.ApplicationRole
Generated SQL
SELECT
[Extent2].[ApplicationRoleId] AS [ApplicationRoleId],
[Extent2].[Name] AS [Name]
FROM [dbo].[ApplicationUsers] AS [Extent1]
INNER JOIN [dbo].[ApplicationRoles] AS [Extent2] ON [Extent1].[ApplicationRo
le_ApplicationRoleId] = [Extent2].[ApplicationRoleId]
WHERE ([Extent1].[ApplicationRole_ApplicationRoleId] IS NOT NULL) AND ([Exte
nt1].[ApplicationUserId] = @EntityKeyValue1)
-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)
If the ApplicationRole object has other navigation properties, they will still be lazy-loaded, without problems.
Here comes a tip that might be helpful. To see when EF is hitting the database, create a console app, add a reference of your solution, and use the context.Database.Log. To see more about logging, take a look at this link https://msdn.microsoft.com/en-us/data/dn469464.aspx