Enabling Migrations in EF core?

扶醉桌前 提交于 2020-01-02 05:32:11

问题


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'.


回答1:


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 command EntityFrameworkCore\Add-Migration firstMigration.




回答2:


Edit your .csproj where you have EF Core 2.0 and add:

<ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>
  1. Open Windows PowerShell
  2. Go to directory where you have EF Core 2.0
  3. Type 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>>



回答3:


in powershell CLI type this --> dotnet ef migrations add InitialMigration

This enables the migration.


This will install the correct core tools

// Package Manger
PM> Install-Package Microsoft.EntityFrameworkCore.Tools -Version 2.0.1
// or this will work inside the CLI Console
dotnet add package Microsoft.EntityFrameworkCore.Tools --version 2.0.1


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

"Microsoft.EntityFrameworkCore.Tools": {
  "version": "2.0.1",  // I corrected this from previous answer for your version
  "imports": [
    "portable-net45+win8+dnxcore50",
    "portable-net45+win8"
  ]
}

Bonus :) To run migrations automatically... in startup.cs of your main application.

// setup the HTTP request pipeline to check and migrate.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{           
    try
    {
        using (var migrationSvcScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
            .CreateScope())
        {
         migrationSvcScope.ServiceProvider.GetService<EFMigrationsMyDBContext>().Database.Migrate();
            // you can also add the data here... let me know if you need I will post it
        }
    }   
    ... // Rest of the startup stuff
}


来源:https://stackoverflow.com/questions/47598844/enabling-migrations-in-ef-core

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!