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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:15:32+00:00 2026-06-18T02:15:32+00:00

I am using Code-First along with Entity Framework 5 and the Repository Pattern .

  • 0

I am using Code-First along with Entity Framework 5 and the Repository Pattern.

I need to get the context from inside some extension methods to access other entities that are not accessible through properties.

Example:

public static class MyClassExtensions
{
    public static void DoSomething(this MyClass mClass)
    {
        // This is what I want to do
        // GetContextSomeWay() is what I need
        // GetRepository is method from my context
        mClass.GetContextSomeWay().GetRepository<SomeRepository>().Where(...);
    }
}

public class MyService
{
    public void DoSomethingOnService(int id)
    {
        MyContext ctx = new MyContext();
        MyClass cl = ctx.GetRepository<MyClass>().Single(c => c.Id == id);
        cl.DoSomething();
    }
}

I thought about two solutions.

  1. Pass the context as a parameter to the extension method
  2. Set the context to each entity using the ObjectMaterialized event

Even though the first approach would work without much hassle, I can’t stop thinking that this is not a good practice.

With the second, besides the work of adding a new property to each one of my classes, I was wondering if that could become a performance issue. Is this an overconcern, or is this valid?

Are there any other solutions to this problem?

  • 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-18T02:15:33+00:00Added an answer on June 18, 2026 at 2:15 am

    Adding Unity and placing your repository there can be a solution.
    You just have a container where the things live that you need,
    at best at the composition root.

    You can access those via the Unity container then.

    Here is a very good tutorial which shows the techniques needed:
    Jason Dollinger on MVVM.
    It is mainly about MVVM, but shows the usage of unity, too.

    The sourcecode is available here:
    MVVM Demo client source code

    Here is more information on those:

    Unity

    Composition Root

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

Sidebar

Related Questions

I'm using Entity Framework (4.3) and code first (along with SQL server 2012 localdb).
I'm using Entity Framework(4.3) Code First Method For My Asp.Net Mvc3 Application.I want to
Framework: I'm using using MVC 3 + EntityFramework 4.1 Code-First. Concept: One Legislation entity
I'm developing a web application using ASP.NET MVC 3 and Entity Framework Code First
I'm using EF 4.3.1 code first to load data from entity Users public class
I'm using EntityFramework code first with migrations. From the package manager console, I'm running
I am using Entity Framework 5 in VS.NET 2012 using a Database First approach.
I am working on a simple web application made using Entity Framework 4.1 Code
I have a database I created using entity code first In that DB I
I'm implementing some code using the java.util.concurrency framework. I will be passing a collection

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.