1

I’m new to the Entity Framework and have a question about ObjectContext lifecycle in a ‘database-first’ WinForm application.

For the sake of illustration, let’s say I am creating a register of animal species.

  • On the left of the form I have a list of animal species (cow, pig, dog etc. – I think these are genus rather than species, but never mind!).

  • On the right of the form I have textboxes, comboboxes etc. to display and allow editing of the selected species’ properties.

When the user selects a species on the left I use:

var species = context.Single(x =>  x.id == speciesID);

then bind the various controls on the right to species.

Some of these properties such as habitat are selected from a combobox (populated from the database when the form loads. I'm using surrogate keys in the database) which means I think I need to keep the same context for the life of the form.

I think there are two problems with this:

  1. The user could make changes to many different species (several hundred exist) during the life of the context making it become bloated.

  2. Ideally I would like to prompt the user when selecting a new species if they wish to save changes to the current one (if dirty) before changing. How then do I discard changes – how do I make the context forget it ever saw the object?

An alternative would be to use a new context each time the user selects a new species, but this would mean repopulating several comboboxes each time so that they are created on the same context and can be bound to.

Am I missing something?

Many Thanks

leppie
  • 115,091
  • 17
  • 196
  • 297
  • I think the best way to discard changes IS to dispose of the context and repopulate it. Since EF uses lazy loading (unless set otherwise), your not going to get anything you don't actually use. The EF context is made to be used via the Unit of Work pattern. – Elad Lachmi Jan 29 '13 at 09:50

2 Answers2

0

I think you will find your answer in this topic:

Undo changes in entity framework entities

Community
  • 1
  • 1
Yoeri
  • 2,249
  • 18
  • 33
0

I would suggest using new context on species change - that would solve problem of undoing changes if needed.

I don't see why you need to re-populate your combo-boxes every time you discard the context. I am assuming that these entities will not be modified at the same time when species is getting modified. So you can either use Attach method on these entities to make them part of current object context (OR you can use the key/id of selected entity to query the entity from database with current object context).

VinayC
  • 47,395
  • 5
  • 59
  • 72
  • Thanks, I like the sound of the first option, but how do I attach all the entities from the comboboxes? Say I had `IQueryable habitats;` Do I need to enumerate and call attach on each? I'm not sure I understand the second option you mention. – Chris Beedie Jan 29 '13 at 11:24
  • @ChrisBeedie, either you can enumerate all habitats and attach them to context or you can only attach the selected habitat on the call to save changes. In either case, you can have generic code that can take care of all combo-boxes. – VinayC Jan 29 '13 at 11:43