无法将Entity Framework Core迁移添加到.NET Standard 2.0项目 [英] Can't add Entity Framework Core migrations to .NET Standard 2.0 project
问题描述
我有一个包含许多项目的解决方案。
其中之一( Domain
)是一个.NET Standard 2.0项目,我在其中制作了EF Core DbContext
我想为其启用数据库迁移的实现。
我看到了各种博客和问答论坛,其中的问题得到了解释,但都没有提议的解决方案似乎对我有用,因为是.NET Core的较新版本,或者(可能是)对于我特定的解决方案配置。
解决方案项目
- 引擎(.NET Core 2.1控制台应用程序)
- Web API(.NET Core 2.1库)
- 应用程序(.NET Core 2.1库)
- 域(.NET Standard 2.0库)
- WindowsService (.NET Core 2.1控制台应用程序)
WindowsService
是启动项目,其中创建 Engine
的实例并将其封装为Windows Service或控制台应用程序(用于调试)运行。
引擎
是真正的核心应用程序,其中 Kestrel
自托管Web服务器的实例被配置为Web API并被实例化。其他组件也会实例化并保持活动状态(UDP侦听器,机器看门狗等)。
WebAPI
具有 Startup
类,但没有 Program
类,因为所有配置和Web服务器启动都在<$ c $内部完成c> Engine.Program.cs 类。
依赖项
- WindowsService =>引擎
- Engine => WebAPI
- WebAPI =>应用程序,域
- 应用程序=>域
第一个尝试是简单地启动 add-从PMC迁移初始
,将 Domain
项目作为目标,但是它变为:
无法创建类型为 MyDbContext的对象。向
项目中添加 IDesignTimeDbContextFactory的
实现,或参见 https://go.microsoft.com/fwlink/?linkid=851728 ,用于在设计时支持
的其他模式。
然后,我在这个问题,但是:
-
add-将My
项目,将变为:WebAPI
(没有Program
类)设置为启动后,从PMC迁移Initial
找不到任何兼容的框架版本
指定的框架'Microsoft.NETCore.App ',未找到
版本。
- 检查应用程序依存关系并定位到安装在以下位置的框架版本:
C:\Program Files\dotnet\ - 安装.NET Core必备软件可能有助于解决此问题:
http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409 - 可以从以下位置安装.NET Core框架和SDK:
https://aka.ms/dotnet-download - 已安装以下版本:
2.0.5,位于[C:\Program位于[C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
2.0的文件\dotnet\共享的Microsoft.NETCore.App]
2.0.6。在[C:\Program Files\dotnet\共享\Microsoft.NETCore.App]中位于7,在[C:\Program Files\dotnet\共享\Microsoft.NETCore中,
2.0.9。 App]
2.1.2,位于[C:\Program Files\dotnet\sharedMicrosoft.NETCore.App]
- 检查应用程序依存关系并定位到安装在以下位置的框架版本:
- <在将其他目标框架(
netcoreapp2.1
)添加到域库后,从PMC p>add-migration Initial
项目文件,导致我遇到与第一次尝试相同的错误。 -
add-migration Initial
在添加对Microsoft.EntityFrameworkCore.SqlServer.Design v1.1.6
的引用后,PMC总是导致与第一次尝试相同的错误。
我该怎么办?
更新
这是删除不必要的库引用后的新 Domain.csproj
文件。
< ItemGroup>
< PackageReference Include = Microsoft.EntityFrameworkCore Version = 2.1.1 />
< PackageReference Include = Microsoft.EntityFrameworkCore.SqlServer Version = 2.1.1 />
< PackageReference Include = Microsoft.EntityFrameworkCore.Tools Version = 2.1.1 />
< PackageReference Include = Newtonsoft.Json Version = 11.0.2 />
< / ItemGroup>
所有项目中的所有库都是最新的。
- 下载并安装适当的
Runtime
和SDK $来自此处的c $ c>-我想您需要
.NET Core当前版本2.1.302
-
Microsoft.EntityFrameworkCore.SqlServer.Design
不再需要包含在SDK中。对于EntityFrameworkCore
,不需要csproj
字段中的CLI
引用,因为 - 确保您
管理NuGet软件包
窗口显示所有已更新。 -
在Web项目实现中的
IDesignTimeDbContextFactory
接口中添加任何位置-它将自动找到并用于EF <$ cPackage Manager控制台$ c中的$ c>添加迁移
(或dotnet ef
...类似物)命令$ c>公共类DesignTimeActivitiesDbContextFactory:IDesignTimeDbContextFactory< ActivitiesDbContext>
{
public ActivitiesDbContext CreateDbContext(string [] args)
{
DbContextOptionsBuilder< ActivitiesDbContext> builder =新的DbContextOptionsBuilder< ActivitiesDbContext>();
var context = new ActivitiesDbContext(
builder
.UseSqlServer( Data Source =(local)\LocalDB; Initial Catalog = DB_name; Integrated Security = True;)
.Options);
返回上下文;
}
}
要从 appsettings
配置文件中读取连接字符串,可以执行以下操作:
公共类DesignTimeActivitiesDbContextFactory:IDesignTimeDbContextFactory< ActivitiesDbContext>
{
public ActivitiesDbContext CreateDbContext(string [] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory() ))
.AddJsonFile( appsettings.Development.json,可选:false);
var config = builder.Build();
var optionsBuilder = new DbContextOptionsBuilder< ActivitiesDbContext>()
.UseSqlServer(config.GetConnectionString( DefaultConnection));
返回新的ActivitiesDbContext(optionsBuilder.Options);
}
}
注意:在上面的代码工厂中将使用连接字符串 appsettings.Development.json
文件中定义的值。连接名称为 DefaultConnection
。换句话说,此设计时间工厂用于 Code First
命令,例如 Add-Migration
, Update-Database
等),并将它们应用于为 Development
环境配置的数据库连接。
I've got a Solution with many projects.
One of them (Domain
) is a .NET Standard 2.0 project where I made my EF Core DbContext
implementation for which I want to enable database migrations.
I saw various blogs and Q/A forums where the problem was explained but none of the proposed solutions seem to work for me because of the .NET Core newer version or (probably) for my particular solution configuration.
Solution projects
- Engine (.NET Core 2.1 Console App)
- Web API (.NET Core 2.1 Library)
- Application (.NET Core 2.1 Library)
- Domain (.NET Standard 2.0 Library)
- WindowsService (.NET Core 2.1 Console App)
The WindowsService
is the Startup project, where an instance of the Engine
is created and encapsulated to run as Windows Service or Console application (for debug).
The Engine
is the real core application, where an instance of Kestrel
self-host Web server is configured as Web API and instantieted. Also other components are instantiated and stay alive (UDP listener, machine watchdog, etc...).
WebAPI
has Startup
class but no Program
class, since all the configuration and the Web server start is done inside Engine.Program.cs
class.
Dependencies
- WindowsService => Engine
- Engine => WebAPI
- WebAPI => Application, Domain
- Application => Domain
The first attempt was to simply launch add-migration Initial
from PMC with Domain
project as target, but it turns to:
Unable to create an object of type 'MyDbContext'. Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.
Then I followed the proposed solution on this question, but:
add-migration Initial
from PMC, after setting MyWebAPI
(with noProgram
class) as startup project, turns to:It was not possible to find any compatible framework version The specified framework 'Microsoft.NETCore.App', version '2.1.0' was not found.
- Check application dependencies and target a framework version installed at: C:\Program Files\dotnet\
- Installing .NET Core prerequisites might help resolve this problem: http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
- The .NET Core framework and SDK can be installed from: https://aka.ms/dotnet-download
- The following versions are installed: 2.0.5 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] 2.0.6 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] 2.0.7 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] 2.0.9 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] 2.1.2 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
add-migration Initial
from PMC, after adding an additional target framework (netcoreapp2.1
) to the Domain library project file, leads me to the same error as the 1st attempt.add-migration Initial
from PMC, after adding a reference toMicrosoft.EntityFrameworkCore.SqlServer.Design v1.1.6
always leads to same error as the 1st attempt.
What should I do?
UPDATE
This is the new Domain.csproj
file after removing the unecessary libraries references.
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>
All libraries in all projects are up to date.
- Download and install appropriate
Runtime
andSDK
from here - I guess you need.NET Core 2.1.302
at the moment Microsoft.EntityFrameworkCore.SqlServer.Design
is not needed anymore as it's included to SDK.CLI
reference incsproj
fiels forEntityFrameworkCore
is not needed as well.- Make sure you
Manage NuGet packages
window shows all updated. Add anywhere in you web project implementation of
IDesignTimeDbContextFactory
interface - it will be found automatically and used for EFAdd-Migration
(ordotnet ef
... analogues) command inPackage Manager Console
public class DesignTimeActivitiesDbContextFactory : IDesignTimeDbContextFactory<ActivitiesDbContext> { public ActivitiesDbContext CreateDbContext(string[] args) { DbContextOptionsBuilder<ActivitiesDbContext> builder = new DbContextOptionsBuilder<ActivitiesDbContext>(); var context = new ActivitiesDbContext( builder .UseSqlServer("Data Source=(local)\LocalDB;Initial Catalog=DB_name;Integrated Security=True;") .Options); return context; } }
To read the connection string from your appsettings
config file you could do the following:
public class DesignTimeActivitiesDbContextFactory : IDesignTimeDbContextFactory<ActivitiesDbContext>
{
public ActivitiesDbContext CreateDbContext(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory()))
.AddJsonFile("appsettings.Development.json", optional: false);
var config = builder.Build();
var optionsBuilder = new DbContextOptionsBuilder<ActivitiesDbContext>()
.UseSqlServer(config.GetConnectionString("DefaultConnection"));
return new ActivitiesDbContext(optionsBuilder.Options);
}
}
NOTE: in the above code factory will use connection string value defined in appsettings.Development.json
file. And connection name is DefaultConnection
. In other words, this disign time factory is used for the Code First
commands like Add-Migration
, Update-Database
, etc...) and will apply them to the database connection configured for Development
environment.
这篇关于无法将Entity Framework Core迁移添加到.NET Standard 2.0项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!