延迟加载 - (实体框架)底层连接意外关闭

时间:2015-08-18 13:47:01

标签: c# entity-framework wcf lazy-loading

我正在尝试使用Entity框架实现Lazy加载。我会先承认我从来没有这样做过,而且我正在努力。所以我正在尝试的一切都是基于我在这里找到的文章。基本上我现在正在获取底层连接意外关闭错误消息。

当我执行WCF跟踪时,它表示在尝试使用DataContractResolver序列化参数http://tempuri.org...Consider时出现错误,如果您正在使用DataContractSerializer或将任何静态未知的类型添加到已知类型列表中。< / p>

所以我研究了DataContractResolver,目前尚不清楚如何实现这一点,并且我不清楚我是如何知道我是否正在使用DataContractSerializer。就像我说我是新手一样。

这是我目前的代码:

 public OMBCase GetOMBCaseByCaseId(long caseId)
    {
        //using (AWCTSDBEntities context = new AWCTSDBEntities())
        AWCTSDBEntities context = new AWCTSDBEntities();
        //{
        try
        {
            context.Configuration.ProxyCreationEnabled = true;
            context.Configuration.LazyLoadingEnabled = true;
            context.Configuration.AutoDetectChangesEnabled = false;

            ((IObjectContextAdapter)context).ObjectContext.CommandTimeout = 120;
            context.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);

            var caseRecQuery = (from c in context.Cases
                                where (c.CaseId == caseId && c.SystemTypeCode == SystemCodes.OMB)
                                select c).OfType<OMBCase>();

            var caseRecord = caseRecQuery.Single();
            var soldierDetails = caseRecord.Soldier;
            var soldierAddress = soldierDetails.Addresses.ToList();
            foreach (var p in soldierDetails.Addresses)
            {
                var soldierAddressPurpose = p.AddressPurposes;
                foreach (var ap in soldierAddressPurpose)
                {
                    var purpose = ap;
                }
            }
            var soldierTelehone = soldierDetails.Telephones.ToList();
            var soldierEmail = soldierDetails.Emails.ToList();
            var soldierContacts = soldierDetails.Contacts.ToList();
            foreach (var ca in soldierDetails.Contacts)
            {
                var contactAddress = ca.Addresses;
                foreach (var cp in contactAddress)
                {
                    var contactAddressPurposes = cp.AddressPurposes;
                }
            }
            foreach (var ce in soldierDetails.Contacts)
            {
                var contactEmails = ce.Emails;
            }
            foreach (var ct in soldierDetails.Contacts)
            {
                var contactTelephone = ct.Telephones;
            }
            var personInterviewed = caseRecord.PersonInterviewed;
            var createdByUser = caseRecord.CreatedByUser;
            context.Dispose();
            return (caseRecord);
            //
        }
        catch (NullReferenceException)
        {
            context.Dispose();
            return (null);
        }
        catch (Exception e)
        {
            context.Dispose();
            WriteToLogFile(e);
            return (null);
            //    }
        }


    }

您可以提供有关从何处出发的任何指导,我们将不胜感激。

提前感谢您提供的任何帮助。

罗德尼

1 个答案:

答案 0 :(得分:0)

在请求实际值之前,“延迟加载”不会填充任何类。这也意味着当context超出范围(或被处置)时,查询中的对象就不好了。

看起来您的程序正在执行此操作:

Logic logic logic; // Logic before getting case
var case = GetOMBCaseByCaseId(caseid);
Logic logic logic; // Logic performed on case

延迟加载最适合这样的场景,其中逻辑在上下文超出范围之前执行:

ProcessOMBCaseByCaseId(Int32 caseid)
{
    using (AWCTSDBEntities context = new AWCTSDBEntities())
    {
        var caseRecQuery = (from c in context.Cases
                            where (c.CaseId == caseId && c.SystemTypeCode == SystemCodes.OMB)
                            select c).OfType<OMBCase>(); 

        Logic logic logic; // Logic performed on case
    }
}