there's probably something wrong on some place in your project. I created simple application based on your pieces and everything works fine. Try it and see, what's different.
First, my simple context:
class MyContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Page>().HasRequired(x => x.Department).WithMany(y => y.Pages).HasForeignKey(x => x.DepartmentId).WillCascadeOnDelete(false);
}
public IDbSet<Page> Pages { get; set; }
public IDbSet<Department> Departments { get; set; }
}
Almost dummy initializer to put some data into database:
class TestInit : DropCreateDatabaseAlways<MyContext>
{
protected override void Seed(MyContext context)
{
base.Seed(context);
var newDepartment = context.Departments.Create();
newDepartment.Name = "test";
context.Departments.Add(newDepartment);
var newPage = context.Pages.Create();
newPage.Department = newDepartment;
context.Pages.Add(newPage);
context.SaveChanges();
// leaving nothing in context
var fullContext = (context as IObjectContextAdapter).ObjectContext;
fullContext.Detach(newDepartment);
fullContext.Detach(newPage);
}
}
And your classes:
public class Page
{
public int ID { get; set; }
public int DepartmentId { get; set; }
public virtual Department Department { get; set; }
}
public class Department
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Page> Pages { get; set; }
}
Now the test:
Database.SetInitializer(new TestInit());
using (MyContext context = new MyContext())
{
Console.WriteLine(context.Pages.Local.Count);
Console.WriteLine(context.Departments.Local.Count);
var page = context.Pages.First();
Console.WriteLine(page.GetType().FullName);
Console.WriteLine(page.Department.Name);
}
You should see (the proxy hash will be probably little bit different):
0
0
System.Data.Entity.DynamicProxies.Page_6D31E91F3B1E767826A19855D3A934B59F85AC161548E4761283C85824520E1D
test
First two zeros are there to be sure, that there are no entities in context. The type of Page class should be proxy, if it's the type from program itself, proxies are not being generated. Probably it's turned off or something is not virtual etc. Last line is actual name of department.
When I run the program, besides other queries, also:
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Name] AS [Name]
FROM [dbo].[Departments] AS [Extent1]
WHERE [Extent1].[ID] = @EntityKeyValue1
is sent to database.