Razor网页数据库连接打开和关闭 [英] Razor web page db connection open and close

查看:444
本文介绍了Razor网页数据库连接打开和关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。

当您编写Database.Open(dbname)时,在剃须刀网页中;您打开与数据库的连接。但是这个连接是自动关闭还是保持打开?



我尝试过的事情:



我正在使用剃刀页面及其帮助程序来创建项目站点,但我不想使用任何外部c#类。

谢谢。

Hello.
In razor web pages when youd write Database.Open("dbname"); you open a connection with the database. But is that connection closing automaticlly or does it stay open?

What I have tried:

I am using razor pages and its helpers to create a project site but i do not want to use any external c# classes.
Thank you.

推荐答案

您应该使用实体框架并将数据库注入代码中。这样,你根本就没有管理数据库连接的状态。



但是,如果你手动完成,你应该在using块中创建连接。那么你所使用的任何课程上的IDisposable都应该与你断开连接。如果你不这样做,没有理由认为你的控制器类会在你的方法中找到对象并进行清理。



如果你正在使用EF,这值得一读:



数据库连接危害与实体框架 - Brent OzarUnlimited® [ ^ ]



基本上,实体框架继续并试图管理你的联系,所以关闭连接基本上是打架。
You should use entity framework and inject your database into the code. That way, you're not managing state of your database connections at all.

But, if you do it manually, you should create your connection in a using block. Then your IDisposable on whatever class you use, should disconnect for you. If you don't do that, there's no reason to think your controller class will find objects in your method and clean them up.

If you're using EF, this is worth reading:

Database Connection Hazards with Entity Framework - Brent Ozar Unlimited®[^]

Basically, entity framework goes ahead and tries to manage connections for you, so closing connections is basically fighting it.


这篇关于Razor网页数据库连接打开和关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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