不应该发生简单单一或默认的超时

时间:2017-12-27 22:26:45

标签: c# entity-framework devexpress

我正在修改devexpress sechudler winforms的约会,并且我的上下文出现了问题

public Appointment getApppointmentById(int AppointmentId)
    {
        Appointment q;
        try
        {

            if (AppointmentId == -1)// if the patient id is -1 we want to create a new record.
            {
                Appointment appointment = new Appointment();
                return appointment;

            }
            q=  _sourceEntities.Appointments
                .Where(w => w.ID == AppointmentId).SingleOrDefault();

        }
        catch (Exception ex)
        {
            throw new EntityContextException("   etApppointmentById(int AppointmentId) failed.", ex);
        }
        return q;
 }

我使用自定义信息方法

调用此方法
 private void dxsourceNetAppointments_AppointmentViewInfoCustomizing(object sender, AppointmentViewInfoCustomizingEventArgs e)
 {
        if (e != null)
        {
            if (e.ViewInfo.Appointment.Id != null)
            {

                _newAppointment = SourceDal.getApppointmentById((int)e.ViewInfo.Appointment.Id);
                _newAppointmentType = SourceDal.getAppointmentTypesById(_newAppointment.AppointID);


                e.ViewInfo.Appearance.BackColor = Color.FromName(_newAppointmentType.Color);
            }
        }
  }

在这里你可以看到我的背景

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using WindowsFormsApplication1.Classes.LookUps;

namespace WindowsFormsApplication1
{
    public class SourceContext : ContextBase
    {
        public SMBASchedulerEntities _sourceEntities = new SMBASchedulerEntities();
     private  SystemDa _systemDB = new SystemDa();

public   Appointment getApppointmentById(int AppointmentId)
    {
        Appointment q;
        try
        {

            if (AppointmentId == -1)// if the patient id is -1 we want to create a new record.
            {
                Appointment appointment = new Appointment();
                return appointment;

            }
            q=  _sourceEntities.Appointments
                .Where(w => w.ID == AppointmentId).SingleOrDefault();

        }
        catch (Exception ex)
        {
            throw new EntityContextException("   etApppointmentById(int AppointmentId) failed.", ex);
        }
        return q;
    }


    public AppointmentType getAppointmentTypesById(int appointmentTypeId)
    {
        AppointmentType _appointmentTypes = new AppointmentType();
        try
        {
            _appointmentTypes = _sourceEntities.AppointmentTypes
                .Where(a => a.ID == appointmentTypeId).FirstOrDefault();
        }


        catch (Exception EX)
        {

        }
        return _appointmentTypes;
    }

} 但我得到的错误是

  

{"执行超时已到期。操作完成之前经过的超时时间或服务器没有响应。"}

此外,它会随机更改消息,说当前连接是打开的,就像实体的RetainSameConnection = True属性一样吗?

我不明白这里出了什么问题。

1 个答案:

答案 0 :(得分:0)

Aluan Haddad从上面的答案中得知,我必须做的就是建立一个上下文工厂,这解决了我的问题

public  class ContextFactory  
{

   public static SourceContext ObtainContext()
   {

        SourceContext context = null;
        string key = Thread.CurrentContext.ContextID.ToString();
        LocalDataStoreSlot slot = Thread.GetNamedDataSlot(key);
        if (slot != null)
        {
            context = (SourceContext)Thread.GetData(slot);
        }
        if (context == null)
        {
            context = new SourceContext();
            if (slot == null)
            {
                slot = Thread.AllocateNamedDataSlot(key);
            }
            Thread.SetData(slot, context);
        }
        return context;
    }

   }
}

然后以与我上面所做的不同的方式调用它。

SourceContext SourceDal = ContextFactory.ObtainContext();

希望这可以帮助遇到此类问题的其他人。