您的项目中的某个地方可能有问题。我根据您的作品创建了简单的应用程序,一切正常。试试看,有什么不同。
首先,我的简单上下文:
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; }
}
将一些数据放入数据库的几乎虚拟初始化程序:
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);
}
}
还有你的课:
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; }
}
现在测试: 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);
}
您应该看到(代理哈希可能会有点不同):
0
0
System.Data.Entity.DynamicProxies.Page_6D31E91F3B1E767826A19855D3A934B59F85AC161548E4761283C85824520E1D
test
可以肯定的是,前两个零是上下文中没有实体的。Page 类的类型应该是代理,如果是程序本身的类型,则不会生成代理。可能它已关闭或某些东西不是虚拟的等。最后一行是部门的实际名称。
当我运行程序时,除了其他查询之外,还有:
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Name] AS [Name]
FROM [dbo].[Departments] AS [Extent1]
WHERE [Extent1].[ID] = @EntityKeyValue1
被发送到数据库。