我正在尝试使用实体框架实现延迟加载。我会坦率地说,我从来没有做过并且很挣扎。因此,我尝试的所有内容都基于我在此处找到的文章。基本上我现在正在获取基础连接意外关闭的错误消息。

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

因此,我已经研究了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);
            //    }
        }


    }


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

在此先感谢您提供的任何帮助。

罗德尼

最佳答案

在请求实际值之前,“延迟加载”不会填充您的任何类。这也意味着,当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
    }
}

关于c# - 延迟加载-(Entity Framework)基础连接意外关闭,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32074250/

10-10 16:37