Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 397
Nigar Jafar Avatar asked Jun 25 '17 13:06

Nigar Jafar


Video Answer


3 Answers

  • 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 :)

like image 135
Sai Avatar answered Nov 14 '22 05:11

Sai


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 :)

like image 40
Basanta Matia Avatar answered Nov 14 '22 03:11

Basanta Matia


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

like image 31
Kudzai Dube Avatar answered Nov 14 '22 03:11

Kudzai Dube