The Type Or Namespace DbContext Could Not Be Found

In this azure tutorial, we will discuss how to fix the error, The Type Or Namespace DbContext Could Not Be Found. Which I got while working with the  Azure Function Project.

The Type Or Namespace DbContext Could Not Be Found

Recently, I was working with one Azure Function Project where I had to implement the Entity Frame Work to interact with the SQL Database using Visual Studio 2019. I have created one Data context class which was supposed to inherit from the DbContext. But while implementing that I got the error The Type Or Namespace DbContext Could Not Be Found.

One point to note down here is I had already installed the EntityFramework NuGet Package but still I got the above error.

Below was the my DataContext class code

using System;
using System.Collections.Generic;
using System.Text;

namespace MyAzurefunctionEF
{
    class UserDataContext: DbContext
    {
      //Implementation logic
    }
}

I got the Error, You can see it below

The Type Or Namespace DbContext Could Not Be Found

The exact error message was as below

CS0246 The type or namespace name ‘DbContext’ could not be found(are you missing directive or an assembly reference?)

The Type Or Namespace DbContext Could Not Be Found [Solved]

To fix the error The Type Or Namespace DbContext Could Not Be Found, you need to follow the below steps

Right click on your Azure Function Project and click on the Manage NuGet Packages.

Search for the EntityFramework and select that and click on the Install button as highlighted below

applicationdbcontext could not be found net core

Then click on the I Accept button to accept the license Agreement.

application dbcontext could not be found net core error

It will now install the EntityFramework NuGet package for your Azure Function Project.

Now same way, search for the Microsoft.EntityFrameworkCore NuGet package and click on the Install button.

application dbcontext could not be found net core

Now same way click on the I Accept button to accept the License Agreement as shown below

system.data.entity not found

Now the Microsoft.EntityFrameworkCore NuGet package will be installed successfully with out any issue

Then The next is, you need to use the below Using Statement in side your class as highlighted below

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;

After following the above mentioned steps, you can able to see, Now there is no error

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;

So this is how you can able to fix the error The Type Or Namespace DbContext Could Not Be Found.

You may also like following the below Articles

Wrapping Up

Well, in this article, we discussed How to fix the error The Type Or Namespace DbContext Could Not Be Found which I got while working with Azure Function with Entity Framework. Hope It will help you to fix your issue !!!

Leave a Comment