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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:05:37+00:00 2026-05-17T15:05:37+00:00

I have extension method that does something like this public static void DoStuff(this ObjectContext

  • 0

I have extension method that does something like this

public static void DoStuff(this ObjectContext context)
{
    using(var newContext = new MyEntitiesContext())
    {
        // do stuff
        newContext.SaveChanges();
    }
    context.SaveChanges();
}

I was wondering if there a way to new a context of the same type as the context passed in instead of specifying MyEntitiesContext?

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-05-17T15:05:37+00:00Added an answer on May 17, 2026 at 3:05 pm

    If you don’t mind reflection:

    var context = Activator.CreateInstance(context.GetType());
    

    Now you either need a base Type or – if you still don’t mind reflection – you can simply call the method by name. Or, since you are using C#4 you could go with dynamic.

    Edit: You could also Go this way:

    public static void DoStuff<T>(this T context) where T : ObjectContext, new()
    {
        using(var newContext = new T())
        {
             // do stuff
            newContext.SaveChanges();
        }
        context.SaveChanges();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an extension method that looks like the following: //[MethodImpl(MethodImplOptions.NoOptimization)] public static IEnumerable<char>
I would like to create a safe sum extension method that would have the
Does Python have extension methods like C#? Is it possible to call a method
Does anyone have a good and efficient extension method for finding if a sequence
I have an extension method on the HttpApplicationState object for getting my IoC container
I have a simple extension method on the int type so I can do
I have a controller that handles file uploads. Ultimately I would like to be
In our ASP.NET MVC project, we have an HtmlHelper extension method to generate a
I need to create an extension method for a GraphicsPath object that determines if
This is my current code that does a MultiField Query ''# Variables used by

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.