无法使用 ASP.net 核心 2 添加迁移
Cannot add migration with ASP.net Core 2
我正在使用 ASP.net Core 2 和 EntityFrameWorkCore 2.1.0 构建一个新项目,并开始构建数据库结构。现在我必须进行第一次迁移以准备好我的数据库,当我在程序包管理器控制台中执行 'Add-Migration Init' 时,我收到了这个错误:
The current CSharpHelper cannot scaffold literals of type 'Microsoft.EntityFrameworkCore.Metadata.Internal.DirectConstructorBinding'. Configure your services to use one that can.
我也试过这个文档 https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations 我得到的消息是:
Unable to create an object of type 'ApplicationDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<ApplicationDbContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.
我也跟着这个 article 没有成功。
如果我在没有 Microsoft.AspNetCore.Identity.EntityFrameworkCore 2.0.2 的情况下尝试使用简单的 2 表结构,我可以让它工作。
所以现在我需要你的帮助...
"Program.cs"中的main方法是怎么安排的?
此错误可能适用于旧的初始化模式。
来自 Asp 净迁移指南:
The adoption of this new 2.0 pattern is highly recommended and is
required for product features like Entity Framework (EF) Core
Migrations to work. For example, running Update-Database from the
Package Manager Console window or dotnet ef database update from the
command line (on projects converted to ASP.NET Core 2.0) generates the
following error:
Unable to create an object of type ''. Add an implementation of 'IDesignTimeDbContextFactory' to the
project, or see https://go.microsoft.com/fwlink/?linkid=851728 for
additional patterns supported at design time.
检查此 link 是否正确实施
我发现一些原因导致我们此时很难使用最新版本的 ASP.net Core 2 进行迁移。
首先,如果您只是从一个根本不是从 ASP.net Core 构建的旧项目迁移,则必须添加一个上下文工厂才能使迁移工作。这是我自己的上下文工厂:
public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
builder.UseSqlServer("Server=(local)\SQLEXPRESS;Database=yourdatabase;User ID=user;Password=password;TrustServerCertificate=True;Trusted_Connection=False;Connection Timeout=30;Integrated Security=False;Persist Security Info=False;Encrypt=True;MultipleActiveResultSets=True;",
optionsBuilder => optionsBuilder.MigrationsAssembly(typeof(ApplicationDbContext).GetTypeInfo().Assembly.GetName().Name));
return new ApplicationDbContext(builder.Options);
}
}
如果您出于架构目的将项目分成多个层,请将上下文工厂添加到存储库层内或 DbContext 所在的同一库中。
其次,在任何尝试添加迁移之前,您必须在存储库库或您的 DbContext 所在的位置设置“设置为启动项目”的选择。
然后,这些是您需要用于迁移的包:
- Microsoft.AspNetCore.Identity.EntityFrameworkCore
- Microsoft.EntityFrameworkCore.Design
- Microsoft.EntityFrameworkCore.SqlServer
就这些!
添加其他包时要小心,因为它们会破坏迁移系统。例如,如果您使用像 URF 这样的工作单元和存储库框架并安装 URF.Core.EF.Trackable -Version 1.0.0-rc2,您将无法再添加迁移.而是使用 URF.Core.EF.Trackable -Version 1.0.0-rc1。我想这也可能发生在许多其他软件包上。
最后,阅读这篇 article 会有帮助。它有点过时,但它可以帮助那里的人。
干杯
确保您已安装所需的 NuGet 包:
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Design
就我而言,在 ApplicationDbContext class 中的启动中,dbcontext 是不同的。
services.AddDbContext<DbContext>
至
services.AddDbContext<ApplicationDbContext>
我正在使用 ASP.net Core 2 和 EntityFrameWorkCore 2.1.0 构建一个新项目,并开始构建数据库结构。现在我必须进行第一次迁移以准备好我的数据库,当我在程序包管理器控制台中执行 'Add-Migration Init' 时,我收到了这个错误:
The current CSharpHelper cannot scaffold literals of type 'Microsoft.EntityFrameworkCore.Metadata.Internal.DirectConstructorBinding'. Configure your services to use one that can.
我也试过这个文档 https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations 我得到的消息是:
Unable to create an object of type 'ApplicationDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<ApplicationDbContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.
我也跟着这个 article 没有成功。
如果我在没有 Microsoft.AspNetCore.Identity.EntityFrameworkCore 2.0.2 的情况下尝试使用简单的 2 表结构,我可以让它工作。
所以现在我需要你的帮助...
"Program.cs"中的main方法是怎么安排的? 此错误可能适用于旧的初始化模式。 来自 Asp 净迁移指南:
The adoption of this new 2.0 pattern is highly recommended and is required for product features like Entity Framework (EF) Core Migrations to work. For example, running Update-Database from the Package Manager Console window or dotnet ef database update from the command line (on projects converted to ASP.NET Core 2.0) generates the following error:
Unable to create an object of type ''. Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.
检查此 link 是否正确实施
我发现一些原因导致我们此时很难使用最新版本的 ASP.net Core 2 进行迁移。
首先,如果您只是从一个根本不是从 ASP.net Core 构建的旧项目迁移,则必须添加一个上下文工厂才能使迁移工作。这是我自己的上下文工厂:
public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
builder.UseSqlServer("Server=(local)\SQLEXPRESS;Database=yourdatabase;User ID=user;Password=password;TrustServerCertificate=True;Trusted_Connection=False;Connection Timeout=30;Integrated Security=False;Persist Security Info=False;Encrypt=True;MultipleActiveResultSets=True;",
optionsBuilder => optionsBuilder.MigrationsAssembly(typeof(ApplicationDbContext).GetTypeInfo().Assembly.GetName().Name));
return new ApplicationDbContext(builder.Options);
}
}
如果您出于架构目的将项目分成多个层,请将上下文工厂添加到存储库层内或 DbContext 所在的同一库中。
其次,在任何尝试添加迁移之前,您必须在存储库库或您的 DbContext 所在的位置设置“设置为启动项目”的选择。 然后,这些是您需要用于迁移的包:
- Microsoft.AspNetCore.Identity.EntityFrameworkCore
- Microsoft.EntityFrameworkCore.Design
- Microsoft.EntityFrameworkCore.SqlServer
就这些!
添加其他包时要小心,因为它们会破坏迁移系统。例如,如果您使用像 URF 这样的工作单元和存储库框架并安装 URF.Core.EF.Trackable -Version 1.0.0-rc2,您将无法再添加迁移.而是使用 URF.Core.EF.Trackable -Version 1.0.0-rc1。我想这也可能发生在许多其他软件包上。
最后,阅读这篇 article 会有帮助。它有点过时,但它可以帮助那里的人。
干杯
确保您已安装所需的 NuGet 包:
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Design
就我而言,在 ApplicationDbContext class 中的启动中,dbcontext 是不同的。
services.AddDbContext<DbContext>
至
services.AddDbContext<ApplicationDbContext>