简体   繁体   English

实体框架-延迟加载失败,但渴望加载正常-如何修复/调试?

[英]Entity Framework - lazy loading failing but eager loading fine - how to fix/debug?

I am using entity framework 4.3.1 against a sql server 2012 database and I have a database with these tables (there are more but the relevant bits are below): 我正在针对SQL Server 2012数据库使用实体框架4.3.1,并且我有一个包含这些表的数据库(还有更多但相关位在下面):

Customer

    CustomerNumber nvarchar(10) not null primary key

SalesDocument

    SalesDocumentNumber nvarchar(10) not null primary key
    CustomerNumber nvarchar(10) not null

I am running the following snippet of code: 我正在运行以下代码片段:

const string docNumber = "111348718";
IQueryable<SalesDocument> docs = factory.CreateSalesDocumentRepository().All;

var ssd = docs.Single(s => s.SalesDocumentNumber.Equals(docNumber));

if (ssd.Customer == null)
    Console.WriteLine("NOOOOOOO");

ssd = docs
    .Include(s => s.Customer)
    .Single(s => s.SalesDocumentNumber.Equals(docNumber));

if (ssd.Customer == null)
    Console.WriteLine("YESSSSSS");

and I get only NOOOOOOO printed. 而且我打印 NOOOOOOO。

So it seems that the lazy loading is not working but eager loading is. 因此,似乎延迟加载不起作用,但急切加载却起作用。

What gives and how on earth can I debug what has gone wrong in this scenario (what are likely causes for this to fail - it feels like a bug in EF to me but I am holding off on declaring that and throwing the whole steaming pile out of the window)? 在这种情况下,给出的内容以及在该情况下我该如何调试(可能导致此失败的原因是什么-对我来说,这就像是EF中的错误,但我一直在声明并把整个蒸堆扔掉的窗户)? Surely the configuration is OK if the second one is working fine...? 如果第二个配置正常,肯定可以配置...?

I don't mean to teach you to suck eggs, but have you enabled LazyLoading in your ObjectContext ? 我不是要教您吮鸡蛋,但是您是否在ObjectContext启用了LazyLoading?

ie: in the ObjectContext constructor 即:在ObjectContext构造函数中

 this.ContextOptions.LazyLoadingEnabled = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM