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

asp.net-mvc entity-framework-6 visual-studio visual-studio-2017

Question

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?

1
4
6/25/2017 2:12:29 PM

Accepted Answer

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

4
6/25/2017 2:27:17 PM

Popular Answer

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



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