Entity Framework Core 添加迁移失败
Entity Framwork Core Add-Migration fails
我正在尝试在我的项目中使用 Ef Core。
结构有点不同,因为我没有在 WebApi.csproj 中使用 EfCore。事实上我有一个不同的dll。和一个 DependenciesResolver.dll 来处理我所有的依赖注入。
在我的 EfCore.dll 我已经安装了两个
Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.SqlServer
现在,当我尝试 运行 命令时(我 运行ning 所在的 dll 是 EfCore.dll)
Add-Migration Name
我明白了:
An error occurred while accessing the IWebHost on class 'Program'.
Continuing without the application service provider. Error: Object
reference not set to an instance of an object. Unable to create an
object of type 'StoreContext'. Add an implementation of
'IDesignTimeDbContextFactory' to the project, or see
https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns
supported at design time.
sln的结构是这样的
WebAPI | EfCore.dll | DependencyResolver.dll 我想保持这种方式,不想允许在我的 WebApi 中使用 EfCore。
这个问题的解决方案是什么?
如果这对 EfCore.dll 有帮助,我有这个。
public sealed partial class StoreContext : DbContext, IStoreContext
{
private string _connectionString;
public StoreContext(string connectionString) : base()
{
_connectionString = connectionString;
Database.EnsureCreated();
}
/// db.tbls
public DbSet<Order> Orders { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.AddOrderConfiguration();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(_connectionString);
}
}
DependencyResolver 这样调用
private DependenciesResolver RegisterInfrastructure()
{
_serviceCollection.AddScoped<StoreContext>(factory => new StoreContext(_connectionString));
return this;
}
然后 DependencyResolver 被 WebApi 调用
请看这个:https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dbcontext-creation
错误消息明确指出 EF Core 工具无法在设计时创建上下文实例。
如果您可以为您的 StoreContext 定义一个不带参数的构造函数,那么您需要告诉工具如何在设计时通过定义一个 class 实现IDesignTimeDbContextFactory 接口。
我正在尝试在我的项目中使用 Ef Core。
结构有点不同,因为我没有在 WebApi.csproj 中使用 EfCore。事实上我有一个不同的dll。和一个 DependenciesResolver.dll 来处理我所有的依赖注入。
在我的 EfCore.dll 我已经安装了两个
Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.SqlServer
现在,当我尝试 运行 命令时(我 运行ning 所在的 dll 是 EfCore.dll)
Add-Migration Name
我明白了:
An error occurred while accessing the IWebHost on class 'Program'. Continuing without the application service provider. Error: Object reference not set to an instance of an object. Unable to create an object of type 'StoreContext'. Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.
sln的结构是这样的
WebAPI | EfCore.dll | DependencyResolver.dll 我想保持这种方式,不想允许在我的 WebApi 中使用 EfCore。
这个问题的解决方案是什么?
如果这对 EfCore.dll 有帮助,我有这个。
public sealed partial class StoreContext : DbContext, IStoreContext
{
private string _connectionString;
public StoreContext(string connectionString) : base()
{
_connectionString = connectionString;
Database.EnsureCreated();
}
/// db.tbls
public DbSet<Order> Orders { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.AddOrderConfiguration();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(_connectionString);
}
}
DependencyResolver 这样调用
private DependenciesResolver RegisterInfrastructure()
{
_serviceCollection.AddScoped<StoreContext>(factory => new StoreContext(_connectionString));
return this;
}
然后 DependencyResolver 被 WebApi 调用
请看这个:https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dbcontext-creation
错误消息明确指出 EF Core 工具无法在设计时创建上下文实例。
如果您可以为您的 StoreContext 定义一个不带参数的构造函数,那么您需要告诉工具如何在设计时通过定义一个 class 实现IDesignTimeDbContextFactory 接口。