I'm getting started with EF Core 2.0, I have a console application targetting .NET 4.6.1 I have a very simple model class, and this context:
public class ContextCore : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["efCoreCon"].ConnectionString);
}
public DbSet<ModelC> Models { get; set; }
}
this is the connection string:
<add name="efCoreCon" connectionString="server=PC-MSHWF\SQLEXPRESS;database=efCoreDB;integrated security=true;" />
I noticed that there's no command for Enable-Migrations
in ef core from the official docs
so I run Add-migration firstMigration
but I got this error:
No migrations configuration type was found in the assembly 'NewConsole'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).
when I tried Enable-Migrations , I got this error:
No context type was found in the assembly 'NewConsole'.
in powershell CLI type this -->
dotnet ef migrations add InitialMigration
This enables the migration.
This will install the correct core tools
Fixing your bug issue:
Look at this SO answer: "You should just need to update the tools section of your project.json file to include this:"
Bonus :) To run migrations automatically... in startup.cs of your main application.
Edit your .csproj where you have EF Core 2.0 and add:
dotnet ef migrations add <<migration's_name>>
. For instance:dotnet ef migrations add Init
. If your startup project is in different folder then you can use--startup-project ../<<other_project_folder>>
Go to the Package Manager Console and install the needed tools with
Install-Package Microsoft.EntityFrameworkCore.Tools
. When it has completed try to use the commandEntityFrameworkCore\Add-Migration firstMigration
.Starting .NET Core 2 using C# 7.1, you can have an asynchronous
Main
method to your app, so you can call all initialization logic before you run the host, right after it has finished building: