基础连接已关闭的连接被意外关闭 [英] The underlying connection was closed The connection was closed unexpectedly

查看:280
本文介绍了基础连接已关闭的连接被意外关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的一个问题,同时使​​用下面的查询时使用实体框架

I facing an issue while using entity framework when using the below query

控制器

TestPlanService.TestPlanServiceClient svclient = new TestPlanService.TestPlanServiceClient();

            try
            {
                return svclient.GetTestPlans().ToList();
            }
            catch (Exception ex)
            {
                string exmess = ex.InnerException.Message;
            }

查询

    using (DataModelContainer db = new DataModelContainer())
                {
                    db.Configuration.LazyLoadingEnabled = false;
                    db.Configuration.ProxyCreationEnabled = false;

                    var query = (from c in db.TestPlans.Include(x => x.TestPoints)
                                 select c);
                    var result = query.ToList();
                    return result;
})

在上面的code虽然我能够从DB与加载的所有子元素返回列表

In the above code though i am able to return the list from the db with all the child elements loaded

模型如下:

public partial class TestPlan
    {
        public TestPlan()
        {
            this.TestPoints = new HashSet<TestPoint>();
        }

        public int TestPlanId { get; set; }
        public string TestPlanName { get; set; }
        public int TestPlanTypeId { get; set; }
        public int RiskTierID { get; set; }

        public virtual ICollection<TestPoint> TestPoints { get; set; }
        public virtual TestPlanType TestPlanType { get; set; }
        public virtual RiskTierMaster RiskTierMaster { get; set; }
    }

public partial class TestPoint
    {
        public TestPoint()
        {
            this.TestPlans = new HashSet<TestPlan>();
        }

        public int TestPointId { get; set; }
        public string TestPointName { get; set; }
        public int TestMethodId { get; set; }
        public Nullable<int> PassThreshold { get; set; }
        public Nullable<int> FailThreshold { get; set; }
        public int OrganizationID { get; set; }
        public string CreatedBy { get; set; }

        public virtual TestMethod TestMethod { get; set; }
        public virtual ICollection<TestPlan> TestPlans { get; set; }
        public virtual OrganizationMaster OrganizationMaster { get; set; }
    }

一个测试计划可以有多个测试点。
我能够返回从服务,但不幸的是在控制器端的结果扔的误差
基础连接已关闭:连接被意外关闭

A test plan can have multiple test points. I am able to return the result from service but unfortunately on the controller side it throw as error as "The underlying connection was closed: The connection was closed unexpectedly"

让我知道,如果有人有这方面的任何解决方案。

Let me know if someone has any solution for this.

推荐答案

@Aron,

您是对的,我想这是因为序列化错误的。
那么对于其他谁正在寻找这个问题,这就是帮了我。

You were right i suppose it was because of serialization error. Well for the other who are searching for this issue, this is what helped me out.

在我的实体模型的.edmx其中.TT文件生成只需加code两线如下文件。这应该为所有的实体来完成这种模式在

In my Entity model .edmx file where the .tt files are generated just add two lines of code as below. This should be done for all the entities within that model

我已经把它应用到下面我的课之一,

I have applied it to one of my class as below,

using System.Runtime.Serialization;    
[DataContract(IsReference = true)]
public partial class TestPlanType
{
    public TestPlanType()
    {
        this.TestPlans = new HashSet<TestPlan>();
    }

    public int TestPlanTypeId { get; set; }
    public string TestPlanTypeName { get; set; }

    public virtual ICollection<TestPlan> TestPlans { get; set; }
}

这篇文章帮我

这篇关于基础连接已关闭的连接被意外关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆