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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:52:45+00:00 2026-05-24T19:52:45+00:00

I am new to Entity Framework 4.0, using it with C#, and currently experimenting

  • 0

I am new to Entity Framework 4.0, using it with C#, and currently experimenting with its features.

What I noticed is that, like with most similar ORMs, it relies on an Context object to deal with the data-manipulation and CRUD statements generation done on the generated entities.

This means that if I want to save the changes back to the database, I always need to be able to have access to a reference to the ObjectContext that has instanciated the entities.

It is fine and all if the context has been created in an accessable scope (same method, for example), but what if I pass an entity or and entity set to a method and want this method to save the changes? It looks like the only easy way is to pass the ObjectContext along with the parameters.

Another solution would be placing the ObjectContext in some sort of global variable.
Needless to say, I find styling and maintainability issues to both of these approaches.

In short, the best way I can imagine is getting a reference to the ObjectContext from the entity or entity set.
I know that it isn’t possible by default.

I have found a method showing adding an extension method to get the ObjectContext from the entity. However, it only works for entities with relationships and calling this method is expensive according to the author.

I was thinking about maybe modifying the T4 template to add a Context property to all my entities and fill it automatically on entities’ instanciation.

I have already modified T4 template once to have Entity Framework enforce Max Length on my generated classes (by following Julie Lerman’s Programming Entity Framework 4 book).
I can’t say I really enjoy the T4 syntax so far, but if that’s the best/only way, so be it…

Has anyone already done so and what would be the best way to handle this and willing to share his T4 template or explain what are the best partial methods or events to hook into to get this done?

Is there any major downside in using such an approach?
I am thinking that having so many references to the ObjectContext may hinder/delay its ability to be recollected by the GC if some of my entities remain in scope but I actually have no use anymore for the ObjectContext.

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-05-24T19:52:49+00:00Added an answer on May 24, 2026 at 7:52 pm

    If you need to pass object context as parameter together with your entities you are doing something wrong.

    Usually context is needed only in well defined layer. All classes from this layer which requires context to their logic can receive the context through some specialized class – context provider (it can also be called service locator). Context provider will hold current context instance in some storage – you can create your own or you can store it per thread, per http request, etc.

    If they need more then one context instance in your classes you can modify your provider to work also as factory.

    Another common approach is combined with dependency injection. You will pass the context to your classes through the constructor (or property) and you will have some bootstraper code which will do all necessary initialization for you (create required instances and pass all dependencies into them). Again you can pass a context or a factory. This is usually used together with IoC containers which will do the plumbing for you.

    Once you have this infrastructure prepared you can pass your entity to the any initialized class from that layer and it will have the context available.

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

Sidebar

Related Questions

I've recently started using Entity Framework migrations and noticed that the database name is
I am writing a new application using the Entity Framework. Where would the EF
I'm new to using LINQ to Entities (or Entity Framework whatever they're calling it)
I am currently working on my own version of membership using Entity Framework 4.0
I'm new to Entity Framework and am expanding an existing codebase. I'm using jQuery
I have an app that is saving to a db (using Entity Framework) and
I'm currently developing an app using ASP.NET MVC3 and Entity Framework 4.1 Code First
New to Linq to Entities. I am using entity framework and linq. In my
I am currently using Entity Framework in an ASP.NET MVC 3 project. And I
I currently have an Entity Framework model that collects data from a legacy database

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.