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?
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
Alternatively, if adding the reference didn't work out:
public Class SampleEF : System.Data.Entity.DbContext
{
}