Nigar Jafar
Nigar Jafar

Reputation: 144

No context type was found in the assembly when running code first migration

While running command enable-migrations in Package Manager Console, got this error:

No context type was found in the assembly 'Vidly'

I am using Visual Studio 2017. How to solve it?

Upvotes: 7

Views: 17595

Answers (4)

Gunesh prasad
Gunesh prasad

Reputation: 21

I have faced this problem and get it done my doing this

  1. uninstall-package EntityFramework
  2. install-package EntityFramework
  3. https://learn.microsoft.com/en-us/ef/ef6/modeling/code-first/workflows/new-database
    (Refer this link and add the "using System.Data.Entity;" to your model classes and follow the link you will get to know)

Upvotes: 2

Kudzai Dube
Kudzai Dube

Reputation: 101

I have encountered this problem a few times and in my case I

  1. uninstalled EntityFramework NuGet package and
  2. installed EntityFrameworkCore NuGet package, entityFrameworkCore.Sqlserver.design and entityframeworkcore.tools.

That fixed the problem for me

Upvotes: 3

Sai
Sai

Reputation: 391

  • As you are using visual studio 2017, I assume you might have used
    the higher version of Entity Framework(6.2.0).
  • Entity Framework requires a context before enable-migrations.

In order to resolve the issue, please find below steps.

  1. Create a class MyDBContext(you can use any name for the class) in the Models folder. Please find below code snippet.

Add this namespace System.Data.Entity for DbContext reference

using System.Data.Entity; 
    namespace Vidly.Models
    {
        public class MyDBContext:DbContext
        {
            public MyDBContext()
            {

            }
            public DbSet<Customer> Customers { get; set; } // My domain models
            public DbSet<Movie> Movies { get; set; }// My domain models
        }
    }
  1. Now open package manager console and type below command to enable migrations.

    enable-migrations -contexttypename MyDBContext (MyDBContext is the name of the class we created in step1)

Hope this helps :)

Upvotes: 29

Basanta Matia
Basanta Matia

Reputation: 1562

Make sure you have set the Default Project, that is present at the top label with a dropdown in your Package Manager Console. And this project should contains your Entity Framework context.

You can check this post to find where Default Project drop down located.

So finaly your code should be like this,

Enable-Migrations -ProjectName MyContextProjectNameHere -StartUpProjectName MyStartUpProjectNameHere -Verbose

Hope it helps :)

Upvotes: 9

Related Questions