[英]Entity Framwork Core Add-Migration fails
I'm trying to use Ef Core in my project. 我正在尝试在我的项目中使用Ef Core。
The structure is a little different, in the sense that I'm not using EfCore insite the WebApi.csproj. 从某种意义上说,我在WebApi.csproj中未使用EfCore,因此结构有所不同。 In fact I have a different dll. 实际上,我有一个不同的dll。 and a DependenciesResolver.dll that handles all my dependency injection. 和一个DependenciesResolver.dll处理我所有的依赖项注入。
In my EfCore.dll I've installed both 在我的EfCore.dll中,我已经安装了两个
Microsoft.EntityFrameworkCore.Tools Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.SqlServer
Now when I try to run the command (the dll in which I'm running is the EfCore.dll) 现在,当我尝试运行命令时(我正在其中运行的dll是EfCore.dll)
Add-Migration Name
I get this : 我得到这个:
An error occurred while accessing the IWebHost on class 'Program'. 在类“ Program”上访问IWebHost时发生错误。 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'. 无法创建“ 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. 将“ IDesignTimeDbContextFactory”的实现添加到项目中,或在设计时参见https://go.microsoft.com/fwlink/?linkid=851728&clcid=0x804 。
The structure of the sln is like this sln的结构是这样的
WebApi | WebApi | EfCore.dll | EfCore.dll | DependencyResolver.dll and I want to keep it this way, don't want to permit using EfCore in my WebApi. DependencyResolver.dll,我想保持这种方式,不想允许在我的WebApi中使用EfCore。
What is the resolution for this issue ? 解决此问题的方法是什么?
If this helps within the EfCore.dll I have this. 如果在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);
}
}
which is called by DependencyResolver like this 像这样被DependencyResolver调用
private DependenciesResolver RegisterInfrastructure()
{
_serviceCollection.AddScoped<StoreContext>(factory => new StoreContext(_connectionString));
return this;
}
and the DependencyResolver is then called by the WebApi 然后由WebApi调用DependencyResolver
Please have a look at this: https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dbcontext-creation 请看一下: https : //docs.microsoft.com/zh-cn/ef/core/miscellaneous/cli/dbcontext-creation
The error message clearly specifies the EF Core tools can't create an instance of your Context at design-time. 错误消息明确指出EF Core工具在设计时无法创建Context的实例。
If you can define a constructor with no parameters for your StoreContext that would work, otherwise you need tell the tools how to create an instance of your context at design-time by defining a class that implements the IDesignTimeDbContextFactory interface. 如果您可以为构造函数定义不带任何参数的构造函数,那么您需要通过定义实现IDesignTimeDbContextFactory接口的类来告诉工具如何在设计时创建上下文实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.