将数据库上下文放在静态类库中的优缺点

编程入门 行业动态 更新时间:2024-10-26 01:18:47
本文介绍了将数据库上下文放在静态类库中的优缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们有一个静态类库来处理重复的多上下文任务。创建一个EF数据库上下文作为静态类的成员是不好的做法吗?

DB上下文是为了理由而被处理的。让他们经常保持连接池流动,可能(这里我猜测)确保表不保持锁定。

所以我邀请麻烦有一个静态类中的数据库环境,或者是我反思这个吗?

解决方案

IMO这绝对不是你想做的事情。

锁定实际上并不是您要在这里遇到的主要问题。 EF只会在保存更改通话的持续时间内锁定(实际上,大多数其他ORM使用的部分承诺交易使用跟踪图表的最大好处之一)。

We have a static class library to deal with repeated multi-contextual tasks. Is it bad practice to create an EF db context as member of a static class?

DB contexts are meant to be disposed of for a reason. Having them disposed frequently keeps the connection pool "flowing" and probably (here I'm speculating) assures that tables don't remain locked.

So am I inviting trouble having a db context in a static class or am I overthinking this?

解决方案

IMO this is definitally not something you want to be doing.

Locking isn't actually the main problem you are going to have here. EF will only lock for the duration of a save changes call (its actually one of the big benefits of using a tracking graph over partially committed transactions which most other ORMs use).

What is going to cause you greif is the tracking graph itself. How EF works (in most cases) is that it keeps a copy of every entity its ever seen and loops through them to find whats changed and run a process called fixups which makes navigation properties work with backlinks. This process loops through every entity the context has ever seen and is called on a bunch of operations (add, attach, delete, save, query and a few others). This means if the tracking graph is large, this process can take quite a bit of time. If you keep your context alive forever the tracking graph size tends toward the size of your database, making it unwieldy and slow.

更多推荐

将数据库上下文放在静态类库中的优缺点

本文发布于:2023-10-26 11:49:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530046.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:放在   上下文   优缺点   静态   类库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!