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

  • Home
  • SEARCH
  • 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 7031421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:48:04+00:00 2026-05-28T00:48:04+00:00

I am working on a base class for my entity framework database context classes.

  • 0

I am working on a base class for my entity framework database context classes. In the base class I need access to the DbContext and in the derived class I need access to the derived DbContext. At the moment I have the following code:

 public abstract class BaseClass: IDisposable   
{
    protected abstract DbContext BaseContext { get; }

    public void Dispose()
    {
        if (BaseContext != null)
        {
            BaseContext.Dispose();
        }
    }
}

public class DerivedClass : BaseClass
{
    DerivedContext context; // public class DerivedContext: DbContext

    protected override DbContext BaseContext 
    {
        get
        {
            return context;
        }           
    }
}

Is this a correct approach?

  • 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-28T00:48:05+00:00Added an answer on May 28, 2026 at 12:48 am

    I would suggest something more like

    public abstract class BaseClass<TContext> : IDisposable 
        where TContext : DbContext
    {
        //not abstract
        protected TContext Context { get; private set; }
    }
    
    public class DerivedClass : BaseClass<DerivedContext>
    {
        ....
    }
    

    In your base class you can access all of the member of DbContext and in your DerivedClass, you can access all of the members of DerviedContext without the need to cast.

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

Sidebar

Related Questions

In the project I'm working on now, we have base Entity class that looks
at my working place (php only) we have a base class for database abstraction.
The current class library I am working on will have a base class (Field)
I'm working with models analogous to the following: class Owner < ActiveRecord::Base has_many :owned
I have the following working tar -pcvf base.tar input/myPacket/my2 --exclude-vcs input/myPacket/my3/*.bmp When i have
While working with templates I ran into a need to make a base class
I am working on a game and I am writing an Entity base class.
I am working on a legacy framework. Lets say 'A' is the base-class and
Here is the way I have my base class working: class AguiWidgetBase { //variables
I'm working on a code base in which we have several configurable types. One

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.