Question

I'm trying to wrap my head around the EntityFramework and how it's supposed to work. I am currently developing a desktop application in C#.NET using EntityFramework 4. The application is heavy on tabs. So there will be a main window and then everything the user does in the application will be done in a separate tab.

So for example, the application will have Orders and Customers.

Lets say the user starts creating a new order and halfway through decides to edit a customer (both will be displayed in separate tabs). When they try to save the customer (in the code I'd be calling ObjectContext.SaveChanges()) EntityFramework will also try to save the order that is still being worked on by the user. I don't want this and would only want to save the customer.

I think I am missing something crucial in my understanding of how EntityFramework is supposed to be used because it doesn't seem like it would ever work properly in an application that allows users to work on multiple entities at a time.

Was it helpful?

Solution 2

I was definitely missing something crucial in my understanding of how EntityFramework is supposed to be used. Or at least the approaches that can be taken with it.

Our application was using a single context for the entire application. The more appropriate approach for a large desktop application is to have a single context per window. This is the approach I'm taking now and I've tested it with one form so far and it's working well.

Here is the link to the blog post that enlightened me. http://www.progware.org/Blog/post/Entity-Framework-the-Context-ComboBoxes-and-Include-Foreign-Key-columns.aspx

OTHER TIPS

You can use multiple Entity objects, but since you can only assign one model per view, a tabbed UI is still a single form, it just hides tabbed elements until an event makes them visible. So, at the top of any view that calls a model, you'll be familiar with this:

@model Web.Project.Name.Models.ViewModel

However, you can use partial views on each of your tabs to invoke the model you want the user to interact with, because partial views can have their own models. In this way, each tab effectively becomes its own form. Your main page(sans a layout view):

@model Web.Project.Name.Models
@{Viewbag.Title = "Orders & Customers";}
...
<div class="tab-1">
@Html.Partial("_OrdersPartial")
</div>
<div class="tab-2">
@Html.Partial("_CustomersPartial")
</div>

Those partials you're loading would each have their own model:

_OrdersPartial.cshtml:

@model Web.Project.Name.Models.OrdersViewModel
@{Viewbag.Title = "Orders & Customers";}
...

In this case, the user interaction with each tab and its partial view will handle each tab as its own form, which is effectively is. It's also important to note that you don't really even need more than one Entity objects to create different models. You can use Entity framework to create multiple models from the same DB.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top