具有 Entity Framework dbcontext 的 Singleton 好吗?
Is Singleton with an Entity Framework dbcontext good?
我的问题太简单了,我有一个想法用一个实例来管理一个DbContext
对象,所以想法是使用单例设计模式。
根据您的经验,单例是否适合与 Entity Framework dbcontext
一起使用以确保只使用一个 dbcontext
实例?
Is singleton good to use with entity framework dbcontext to ensure that one dbcontext instance is used?
这不仅是个坏主意,而且行不通。它不是线程安全的,并发使用时会产生异常。长期存在的 DbContext 往往会在其 Change Tracker 中积累大量对象,从而降低性能并浪费资源。
The lifetime of a DbContext begins when the instance is created and
ends when the instance is disposed. A DbContext instance is designed
to be used for a single unit-of-work. This means that the lifetime of
a DbContext instance is usually very short.
我的问题太简单了,我有一个想法用一个实例来管理一个DbContext
对象,所以想法是使用单例设计模式。
根据您的经验,单例是否适合与 Entity Framework dbcontext
一起使用以确保只使用一个 dbcontext
实例?
Is singleton good to use with entity framework dbcontext to ensure that one dbcontext instance is used?
这不仅是个坏主意,而且行不通。它不是线程安全的,并发使用时会产生异常。长期存在的 DbContext 往往会在其 Change Tracker 中积累大量对象,从而降低性能并浪费资源。
The lifetime of a DbContext begins when the instance is created and ends when the instance is disposed. A DbContext instance is designed to be used for a single unit-of-work. This means that the lifetime of a DbContext instance is usually very short.