How to inherit from the DbContext Class

c# entity-framework

Question

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?

1
2
4/21/2012 8:15:52 AM

Accepted Answer

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

5
4/21/2012 6:22:06 AM

Popular Answer

Alternatively, if adding the reference didn't work out:

public Class SampleEF : System.Data.Entity.DbContext 
{

}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow