Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net “我怎样才能解决问题?”;连接没有关闭。连接的当前状态:正在连接。”;_.net_Asp.net Mvc_Database_Entity Framework_Entity - Fatal编程技术网

.net “我怎样才能解决问题?”;连接没有关闭。连接的当前状态:正在连接。”;

.net “我怎样才能解决问题?”;连接没有关闭。连接的当前状态:正在连接。”;,.net,asp.net-mvc,database,entity-framework,entity,.net,Asp.net Mvc,Database,Entity Framework,Entity,我用实体框架开发了一个应用程序。有时,我会得到 连接没有关闭。连接的当前状态:正在连接 我在互联网上做了一些研究,但我搞不懂。如果你能帮忙,我将不胜感激 家庭控制器: public ActionResult Index() { //return View(CacheHelper.getIndexNotesFromCache()); //Yazıları son değiştirilme tarihine göre sırala ve v

我用实体框架开发了一个应用程序。有时,我会得到

连接没有关闭。连接的当前状态:正在连接

我在互联网上做了一些研究,但我搞不懂。如果你能帮忙,我将不胜感激

家庭控制器:


        public ActionResult Index()
        {

            //return View(CacheHelper.getIndexNotesFromCache()); //Yazıları son değiştirilme tarihine göre sırala ve view'e gönder
            return View(noteManager.ListQueryable().Include(x=>x.Owner).Include(x=>x.Comments).Include(x=>x.Category).Include(x=>x.Likes).Where(x => x.IsDraft == false && x.IsApproved == true).OrderByDescending(x => x.ModifiedOn).Take(10).ToList());
        }
我的笔记实体:

public class Note : MyEntitesBase
{       
    public string Tittle { get; set; }
    public string Text { get; set; }
    public bool IsDraft { get; set; }
    public int LikeCount { get; set; }
    public int CategoryID { get; set; }

    public virtual EvernoteUser Owner { get; set; } 
    public virtual List<Comment> Comments { get; set; } 
    public virtual Category Category { get; set; } 
    public virtual List<Liked> Likes { get; set; } 

    public Note()
    {
        Comments = new List<Comment>();
        Likes = new List<Liked>();
    }

}

我的数据库上下文:

public class DatabaseContext :DbContext 
{
   public DbSet<EvernoteUser> EvernoteUsers { get; set; }
   public DbSet<Note> Notes { get; set; }
   public DbSet<Comment> Comments { get; set; }
   public DbSet<Category> Categories { get; set; }
   public DbSet<Liked> Likes { get; set; }

   public DatabaseContext()
   {           
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<DatabaseContext,Configuration>()); 
        Database.Initialize(force: false);

    }

}
公共类数据库上下文:DbContext
{
公共DbSet EvernoteUsers{get;set;}
公共DbSet注释{get;set;}
公共DbSet注释{get;set;}
公共数据库集类别{get;set;}
公共DbSet喜欢{get;set;}
公共数据库上下文()
{           
SetInitializer(新的MigrateDatabaseToLatestVersion());
初始化(强制:false);
}
}
您可以参考此

手动打开连接并不能解决问题。它只会更早地打开连接。否则,该连接将在第一次下载记录集时打开。通过添加


池=假

我的猜测是,当您从数据库中访问信息时,这是一个关键点。您不能关闭连接。首先,您正在序列化实体,这意味着对于10个注释中的每一个注释,序列化程序都要延迟加载所有者、类别以及所有注释和类似内容。通常情况下,这会在DbContext被释放时表现出来,而不是在连接状态下。无论哪种方式,您都可以重新考虑将实体发送到视图,因为如果您不这样做,它的性能会更好,并且可能会避免类似的问题。@StevePy我这样做了,但问题仍然没有完全解决。我已经查看了主题“Pooling=false”。我将其添加到Webconfig,但问题没有解决
public class DatabaseContext :DbContext 
{
   public DbSet<EvernoteUser> EvernoteUsers { get; set; }
   public DbSet<Note> Notes { get; set; }
   public DbSet<Comment> Comments { get; set; }
   public DbSet<Category> Categories { get; set; }
   public DbSet<Liked> Likes { get; set; }

   public DatabaseContext()
   {           
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<DatabaseContext,Configuration>()); 
        Database.Initialize(force: false);

    }

}