首页 \ 问答 \ 实体框架和数据库连接(Entity Frameworks and database connections)

实体框架和数据库连接(Entity Frameworks and database connections)

我正在调试Jaroslaw Kowalski提供者包装。 在调试它时,我发现实体框架在每次查询后关闭连接。 在与数据库的每次交互中,在应用程序的生命周期中,打开数据库连接,并在执行查询后关闭连接

我的问题是:

  • 默认情况下,底层提供者是否执行连接池。
  • 如果我提供自己的资源池,那么我将如何知道何时关闭连接。 目前我正在使用Thread.GetDomain()。ProcessExit事件注册并在事件触发时关闭连接。 这种方法好吗?

困扰我的事情是Entity Framework本身正在关闭连接。 所以我有点犹豫是否想要集中连接,或者在特殊情况下可能会导致问题。


I was debugging Jaroslaw Kowalski provider wrappers. While debugging it I observed that the Entity Framework closes the connection after every query. On every interaction with database, during the lifetime of application, a database connection is opened and after performing the query the connection is closed

My questions are:

  • Whether the underlying provider do connection pooling by default.
  • If I provide my own pooling then how will I know when to close the connection. At the moment I am registering with Thread.GetDomain().ProcessExit event and closing the connection when the event fires. Is this approach good?

The thing that is bothering me is that Entity Framework itself is closing the connection. So I am a little hesitant to pool the connection or it may cause problems in exceptional scenarios.


原文:https://stackoverflow.com/questions/1928172
更新时间:2023-09-15 08:09

最满意答案

推荐这个结构:

  1. 用户名
  2. 地址类型
  3. 地址

因为它在第3 NF。

在单个表中简单使用多个列的缺点是:

  • 每当过滤地址时都需要记住“空句柄”
  • 随着时间的推移,随着新地址类型的增加,范例可能会超越意义; 但转换成本太高而无法考虑。

应添加具有FK关系的AddressType表,以防止意外输入看起来有效但不是的AddressType。

最重要的是,从理论上讲,每个AddressType都是一个单独的表,在UserId上链接到用户表; 但对于提出的问题,这确实看起来有点过分。


Recommended is this structure:

  1. UserId
  2. AddressType
  3. Address

because it is in 3rd NF.

The disadvantages of simply using multiple columns in a single table are these:

  • The need to remember to "null-handle" whenever filtering on Address
  • Over time, the paradigm may expand beyond sense as new address types are addded; but a conversion becomes too costly to contemplate.

An AddressType table should be added with a FK relationship to prevent accidental entry of AddressTypes that look valid, but aren't.

Best of all, theoretically, would be a separate table for each AddressType, linked on UserId to the user table; but that really does look like overkill for the problem as presented.

相关问答

更多
  • Sql Server具有FileStream功能,允许您存储不适合标准varchar(max)字段的数据。 还有一个名为FileTables的选项(使用FILESTREAM ),它允许您将文件存储在文件系统上,但可以直接从T-SQL访问它。 它很光滑,但我的同事和我发现学习曲线非常陡峭; 你必须习惯很多小怪癖。 Sql Server has a FileStream feature that allows you to store data that doesn't fit in a standard va ...
  • 推荐这个结构: 用户名 地址类型 地址 因为它在第3 NF。 在单个表中简单使用多个列的缺点是: 每当过滤地址时都需要记住“空句柄” 随着时间的推移,随着新地址类型的增加,范例可能会超越意义; 但转换成本太高而无法考虑。 应添加具有FK关系的AddressType表,以防止意外输入看起来有效但不是的AddressType。 最重要的是,从理论上讲,每个AddressType都是一个单独的表,在UserId上链接到用户表; 但对于提出的问题,这确实看起来有点过分。 Recommended is this st ...
  • 这是一个非常普遍的问题。 它的主要依赖于你的代码的基础设施。 您的类和模型的定义方式和应用程序的动态。 其次,重要的是要考虑运行应用程序的服务器的资源。 你有多少内存可用,多少磁盘空间,这样你可以考虑哪些对于应用程序更好。 最后但并非最不重要的一点是,需要考虑将所有这些资源存储在内存中需要执行多少操作。 内存是易变的,所以如果你的应用程序重新启动,你将不得不再次实例化所有的类,也许这是很多工作。 恢复时,作为优化是保持经常查询的内存对象的非常好的选择(这就是缓存的全部内容),但是必须考虑到以前的所有内容。 ...
  • 首先,你真的不想那样做。 RDBMS中的列是原子的,因为它包含一条且只有一条信息。 尝试在列中存储多个数据是违反第一范式的。 如果您绝对必须这样做,那么您需要将数据转换为可以存储为单个数据项(通常是字符串)的表单。 您可以使用PHP的serialize()机制,XML解析(如果数据恰好是文档树),json_encode()等。 但是,您如何有效地查询这些数据? 答案是你不能。 此外,如果其他人在以后接管你的项目,你真的会惹恼他们,因为数据库中的序列化数据是可怕的。 我知道因为我继承了这样的项目。 我提到你真 ...
  • 一切都是一种观点,但你会发现很多'不是这个答案。 为什么? 基本模式是MV *。 我强烈建议阅读Essential JavaScript Design Patterns的这一章 。 最后,它取决于您想要存储多少数据。 如果它变得复杂,将其移动到模型或控制器。 有用于此目的的选项( 在此处阅读有关HTML数据属性的更多信息 )。 如果你存储的不仅仅是一个简单的数字或字符串,我不会推荐它。 反对的原因: 如果其他人也操纵你的代码,那么遵循设计模式会更容易,所以每个人都对你的代码有相同的理解。 将数据存储在模型中 ...
  • 始终将用户输入存储在未编码的数据库中,并始终在输出之前对来自数据库的用户输入进行编码。 您还应该在持久化之前过滤/验证用户输入。 输入:用户输入 - >验证/过滤 - >坚持到数据库 输出:来自数据库的内容 - >编码 - >输出到客户端 这是使用和重用用户数据的唯一理智方式。 另请参阅http://msdn.microsoft.com/en-us/library/t4ahd590%28v=vs.80%29.aspx#cpconbestsecuritypracticesforwebapplicationsa ...
  • 微软的官方声明是你在使用IDENTITY时应该会遇到差距,所以如果你的删除导致这个差距,我会推断它绝对没问题。 没有理由填补空白 https://connect.microsoft.com/SQLServer/feedback/details/739013/failover-or-restart-results-in-reseed-of-identity 由于这是一个与切线相关的主题的非常长的主题,相关引用如下: 正如在线书籍中针对以前版本的SQL Server所记录的那样,身份属性并不能保证没有间隙,对于 ...
  • 继续,这是Hibernate使用的一种广泛采用的做法。 对于内存受限的设备: 那么根本不要使用嵌入式数据库。 尝试使用ThoughtWorks库XStream ,它将对象序列化/反序列化为可存储在文件中的XML / jSon。 非常有效的解决方案,具有较小的记忆足迹。 Go on, it's a widely adopted practice used by Hibernate for example. For a memory constrained devices: Then do not use em ...
  • 由于您的表不会无休止地增长并且直观地理解列的内容比保存一些内存/硬盘空间或毫秒来查询结果更重要: 我会在每个工作日创建一个位/布尔列: Mo Tu We ... Su 这比“tbl_weekdays”的连接表更好,因为你必须确保没有人将同一个工作日分配给同一个事件两次。 您提到的位编码解决方案非常优雅,但是需要有关位的含义的隐含知识。 单独的列使这些知识显式化并简化SQL查询。 顺便说一句:这个解决方案只能工作,因为不同值的基数低(只有7),否则列数会爆炸。 Since your table will no ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)