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 8778083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:26:22+00:00 2026-06-13T19:26:22+00:00

I am just beginning to learn Entity Framework (starting with 5.0) and running into

  • 0

I am just beginning to learn Entity Framework (starting with 5.0) and running into some issues. After watching a few videos on Pluralsight, I decided to get creative and created an abstract base class that inherited from DbContext and exposed a single “DataContext” property that was a DbSet of a generic type. Something like this:

public abstract class BaseDataContext<E> : DbContext
     where E : CustomEntity
{
      public DbSet<E> DataContext {get;set;}

      public BaseDataContext()
      {
           Database.SetInitializer<BaseDataContext<E>>(null);
      }
 }

This seemed to be working great until I started dealing with entities that had relationships with each other (parent/child). What was occurring is when I tried to add a new child entity to the database via a data class that inherited BaseDataContext is instead of just attaching the reference (meaning, taking the child and say “your parent key is 123”), it created another record with a new key for the parent and all other fields duplicated and made this new record the parent of the child. I tried just setting the key property of my child (nulling out the property that was exposed with the data), but that didn’t change anything. I found a thread on SO (here) that said it wasn’t a good idea to do multiple data contexts like what I was attempting to do and seemed to explain exactly what was happening.

My question for this is, with the popularity EF seems to be gaining for everyone, is this the common approach? To create a single data context that is then passed around with constructors everywhere? Or is there a simpler way that is more common? This would especially apply if things get into background threads since passing a context there might get difficult.

Thanks in advance!

  • 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-13T19:26:24+00:00Added an answer on June 13, 2026 at 7:26 pm

    You get the most out of EF, as any tool, when you use as intended. So it’s good you ask. Entity Framework, especially code-first, is all about persistence ignorance (explained here for example). I don’t know what your comment “like you would’ve done before EF/L2S” refers to, but it sounds like active record. That’s not the way to go.

    The context is responsible for materializing entities from the store, tracking changes and saving changes to the database. In more detail: the context manages the unit of work (tracking changes) and it’s DbSet members serve as basic repositories (materializing and saving). The entity classes themselves know nothing about this. (As a side note: in the ObjectContext API they do, but they can be coded against as persistence ignorant).

    Now about “multiple contexts”. The question you refer to is about multiple context instances of the same context type. Your question is about multiple context types (and, necessarily, multiple instances). Let’s sort this out:

    • Number of context types: It is common to have a very limited number of context types, often even just one. Why? Because a context covering a large part of the database is able to retrieve and persist many different object graphs. So it serves a large number of use cases, present and future, stable and changing, but it is stable itself. It only changes when the database changes, not when the application changes. (I should say though that there are advocates of one context type per use case, but I’ve found it to be inevitable that they have a large number of classes in common, very confusing).
    • Number of context instances: this issue cannot be fully covered in a few words, but it should be kept in mind that a context (1) caches data and (2) tracks and stores changes. So long-lived contexts soon contain stale data and they grow in size as changes occur. The consensus is to keep contexts “short” lived, but what is short? It could be one instance per http request, one per service call, one per (business) transaction, but also one per form in an MDI interface, one per thread… See a useful discussion on Linq-to-Sql contexts here, the same goes for EF contexts (or NHibernate sessions for that matter). One thing is clear: don’t use one static context, as some people are tempted to do.

    So be as creative as you can, experiment as much as you can. Use EF as it is intended to and have lots of fun!

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

Sidebar

Related Questions

I'm beginning to just learn some SVG (and javascript, I suppose), and I'm having
I'm just beginning to learn about file compression and I've run into a bit
I'm just beginning to learn CSS (and XHTML) and I ran into a problem
I'm just beginning with C++, so I'm looking some code to learn. I found
We are just beginning to learn and evaluate Mercurial, due to an increasing number
I'm just beginning to learn python. I wrote an example script to test OOP
I am just beginning to learn VHDL in modelsim, so i apologize in advance
I am just beginning my journey into web development and I have a very
I am just beginning to get into desktop application development, and have chosen C#
So I am just beginning recently developing some simple apps for the iphone. I

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.