How to inherit from the DbContext Class

10,125

Firstly Add Reference to System.Data.Entity in your project by Right Click your project in Solution explorer and Select Add Reference. Then use the using statement as below:

using System.Data.Entity;
using System.Data.Entity.Infrastructure;

Then write your Context Class as:

public Class SampleEF : DbContext 
{

}

Ref: Using DbContext in EF 4.1 Part 1: Introduction and Model

Share:
10,125
V G S Naidu
Author by

V G S Naidu

I am Microsoft.NET Developer..

Updated on June 04, 2022

Comments

  • V G S Naidu
    V G S Naidu almost 2 years

    I am working on MVC and in the model I created a class that uses Entity Framework. I am trying to inherit from DBContext class but it is showing an error saying: "The type or Namespace name DBContext doesn't exist". I also added the "System.data.Entity " namespace.

    Example like :

     public Class SampleEF :DBContext //Showing error
    {
    
    }
    

    Can you please tell me how to use the DBContext class to work with EF?

  • Niranjan Singh
    Niranjan Singh about 12 years
    it's is good. and one thing consider to upvote and accept the answer here if it help you to solve your problem, it will encourage other to help..