Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9198989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:25:11+00:00 2026-06-17T22:25:11+00:00

I’m new to the Entity Framework and have a question about ObjectContext lifecycle in

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:25:12+00:00Added an answer on June 17, 2026 at 10:25 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Entity Framework and am using Database First. I have inherited a
I have created a new application using Entity Framework 4.3 database migrations. The migrations
When adding a new Entity Framework object to a database, how could one have
I am new to both MVC and Entity Framework and I have a question
iam new to Entity framework i have learned about using SP and how to
I have a litte problem about save new record to database by using Entity
I have a question about Entity Framework. Please answer if you know answer on
I heard about the new entity framework for .net, and decided to modify my
I'm new to the entity framework and I'm really confused about how savechanges works.
Let's say i have a table named User. When I use the Entity Framework

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.