Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework + SQL Server Compact + WPF/WinForms = Sluggish UI?

Entity Framework doesn't allow sharing the same entity between multiple database contexts. Therefore, I have to use only one database context in a GUI application (be it WPF or WinForms), because entities need to interact with each other.

SQL Server Compact doesn't allow sharing the same database connection between multiple threads. If I try creating a connection on one thread and running SQL query on another, my application is likely to crash.

Therefore, I have to create EF database context on one thread and run all queries on that thread. I've used GUI thread for that, because almost all queries are very fast. However, now I have a slow query and want to show an animated progress bar while it's being executed.

But I can't do that, because if I run a query on a different thread, my app crashes with AV. Furthermore, EF seems to complain if I run multiple queries simultaneously, even without SQL CE involved. Moving all queries to a different thread, covering all code with crazy amounts of async/await, callbacks, locks and other threading stuff sounds scary too, as I want to keep the code simple if it's possible.

Question: What is the correct way to work with EF database contexts and SQL Server Compact in a multi-threaded GUI application? Is there any way to offload individual queries to a different thread without making the whole application asynchronous, i.e. is there a simple way to do it?

like image 761
Athari Avatar asked Mar 26 '14 15:03

Athari


People also ask

How do I use SQL Server Compact Edition with Entity Framework?

I try to use SQL Server Compact Edition with Entity Framework in Visual Studio 2008 SP1. Here's what I do: 1) I create a new project, of type Console Application. 2) I right-click on the project, select Add->New Item. 3) I choose to add a Local Database called Something.sdf

Does Entity Framework support data binding in WinForms?

Although all the great features of the Entity Framework can be used in WinForms applications, there is little support for data binding. This is unfortunate since WinForms is still the platform of choice for many data-oriented business applications, and data binding is an essential part of WinForms development.

How do I install Entity Framework on Windows Forms?

Select Windows in the left pane and Windows FormsApplication in the right pane In Solution Explorer, right-click on the WinFormswithEFSample project Select Manage NuGet Packages… In the Manage NuGet Packages dialog, Select the Online tab and choose the EntityFramework package

Can I use the efwinforms data layer assembly in other projects?

The data layer assembly could be used in other projects and maintained separately from the UI projects. I think the EFWinForms library is an interesting project that makes using the ADO.NET Entity Framework in WinForms a lot easier (and I hope after reading this article you agree).


1 Answers

SQL Server CE supports multithreading. But its objects like SqlCeConnection or SqlCeTransaction are not thread-safe. Each thread should use a separate connection. An Entity Framework DataContext instance is designed to last for one unit of work (bussiness transaction). The recommendation is a context per a form.

You can either redesign your application and store/transfer data using DTOs. Or you can use Attach/Detach features of Entity Framework (here or here). Or combine both.

like image 56
Alexandr Nikitin Avatar answered Oct 02 '22 06:10

Alexandr Nikitin