Dispose Entity DbContext in WinForms

entity-framework entity-framework-6 winforms

Accepted Answer

Sure. This is referred to as a "related instance," meaning that the context remains "connected" to the database while you save the same entities that you previously retrieved from the database.

This is a typical layout for somewhat transient edit dialogue windows in rich client applications like Windows Forms applications. That's precisely what you're doing in this situation.

Actually, the context doesn't maintain a connection open to the database. After each database contact, the connection is closed, which is why SQL Profiler logging is used.

One thing to think about Although it's unlikely that users would alter the same data at the same time, it could be a good idea to introduce favourable concurrency control. This is quite simple using EF.

6/17/2016 6:58:34 PM

Popular Answer

Here are some connected entities to keep in mind when your DbContext has a long live time:

  • Each entity that is obtained from the SQL Server will be stored in the first level cache in memory (RAM USAGE).

  • You can get concurrency problems if the data changed from another DbContext.

  • You may receive a dirty read if your SQL Server TRANSACTION ISOLATION LEVEL is READ UNCOMMITTED.

  • The application may run slower in general and you may experience performance issues while changing an entity if there are many entities that are loaded in the DbContext (thousands), since the EF must track them all.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow