K.Z
K.Z

Reputation: 5075

Apply Migrations for EF 6 Code First - no context type was found

I am working on WCF .NET Framework 4.8 project and its WCF is self-hosted in a Console application.

I have set up the defined POCO classes and DbContext in their own separate project that is referenced by the WCF console app.

I have declared the Connection String for the DbContext in the app.config within the main console app.

When I try to execute the EF Code First commands like EnableMigrations and Add-Migration they fail to execute with the error:

No context type was found in the assembly 'MAL.App.ConsoleHost'

Package Manager Console Error

console application App.Config

<entityFramework>
    <providers>
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    </providers>
</entityFramework>
<connectionStrings>
   <add name="AppDbContext" connectionString="Data Source=MySQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

Data Class Library

public class AppDbContext: DbContext
{
    public DbSet<Album> Albums { get; set; }
    public DbSet<AlbumType> AlbumTypes { get; set; }
    public DbSet<Artist> Artists { get; set; }
}

One POCO class

public class Album
{
    public Guid AlbumID { get; set; }
    public string AlbumName { get; set; }
    public Guid ArtistID { get; set; }
    public Guid AlbumTypeID { get; set; }
    public int Stock { get; set; }

    public Artist Artist { get; set; }
    public AlbumType AlbumType { get; set; }

}

Console Main Program

static void Main(string[] args)
{
    ServiceHost hostMusicService = new ServiceHost(typeof(MyWebServices));
    hostMusicService.Open();

    Console.WriteLine("Web Services Started. Press [Enter] To Exit  ");
    Console.ReadLine();

    hostMusicService.Close();
}

Upvotes: 2

Views: 2970

Answers (3)

Long Chu Hai
Long Chu Hai

Reputation: 31

try this command may help.

EntityFrameworkCore\enable-migrations

Upvotes: 0

Chris Schaller
Chris Schaller

Reputation: 16554

This error occurs when your DbContext is defined in a separate project to your main runtime and your main runtime is set as the Default Project in the solution.

When the Package Manager Console is first opened, it assumes that the Solution Default Project is the project that you to execute commands against like Enable-Migrations.

You could change the Default Project of the solution, but a simpler way around it is to change or set the default project in the Package Manager console to the project that has your AppDbContext definition, as this is the project that you want the Migrations to be managed in.

Changing the default project in the package manager console

Here's the catch though, still leave your Default Project in the Solution as your console app.

  • The solution Default Project is more than just the project that will execute when you hit F5 to run, this is the project that is used any time that Visual Studio needs to access an app/web config file at runtime.
  • This was the same for the previous versions of EF with the EDMX design surface and even before that with DataSet designers, the connection strings from the Default Project config file were the ones the VS used at design time.

You can also use the -ProjectName argument to most of the EF CLI commands to pass in the name of the project instead of using the default. For brevity, I usually reserve this for scenarios where there are multiple projects that have contexts or especially when there are multiple contexts in the same project. You don't want to have to type out the project name every time you want to Add-Migration or Update-DataBase or for any other scaffold commands like this.

To get information about the supported arguments, use the -? switch after the command in the console:

PM> Enable-Migrations -?

NAME
    Enable-Migrations
    
SYNOPSIS
    Enables Code First Migrations in a project.
    
    
SYNTAX
    Enable-Migrations [-ContextTypeName <String>] [-EnableAutomaticMigrations] [-MigrationsDirectory <String>] [-ProjectName <String>] [-StartUpProjectName <String>] [-ContextProjectName <String>] [-ConnectionStringName <String>] 
    [-Force] [-ContextAssemblyName <String>] [-AppDomainBaseDirectory <String>] [<CommonParameters>]
    
    Enable-Migrations [-ContextTypeName <String>] [-EnableAutomaticMigrations] [-MigrationsDirectory <String>] [-ProjectName <String>] [-StartUpProjectName <String>] [-ContextProjectName <String>] -ConnectionString <String> 
    -ConnectionProviderName <String> [-Force] [-ContextAssemblyName <String>] [-AppDomainBaseDirectory <String>] [<CommonParameters>]
    
    
DESCRIPTION
    Enables Migrations by scaffolding a migrations configuration class in the project. If the
    target database was created by an initializer, an initial migration will be created (unless
    automatic migrations are enabled via the EnableAutomaticMigrations parameter).
    

RELATED LINKS

REMARKS
    To see the examples, type: "get-help Enable-Migrations -examples".
    For more information, type: "get-help Enable-Migrations -detailed".
    For technical information, type: "get-help Enable-Migrations -full".

PM> 

Upvotes: 1

K.Z
K.Z

Reputation: 5075

I have found the answer. You need to add the Data Class Library Project Name in Migration as below;

- Enable-Migrations -ProjectName MyContextProjectNameHere -StartUpProjectName MyStartUpProjectNameHere -Verbose

- add-migration Initial -ProjectName MyContextProjectNameHere

-  update-database -ProjectName MyContextProjectNameHere

Upvotes: 0

Related Questions