我有 ASP.NET Core 1.1 项目,我正在尝试将其转换为 v2.1。我找到了IDesignTimeDbContextFactory实现,但我不明白我应该在哪里创建这个工厂的实例以及如何将它用于迁移正常工作。现在在创建/删除迁移之前,我必须清理并重建解决方案,因为如果没有,我会收到以下消息:Unable to create an object of type 'MyDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<MyDbContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time。所有上下文数据都在单独的项目中。如何使用IDesignTimeDbContextFactory实现或我做错了什么?
我对这个主题进行了广泛的搜索,并发现我想要做的事情的结果为零。
从高层次来看,这就是我想做的:
这有什么意义呢?
嗯,上面的断言已经描述得差不多了;数据的完整性。此项目中的迁移是在应用程序启动时使用Database.Migrate(). 我想确保现有数据没有丢失/损坏。
我遇到的所有集成测试示例几乎都Database.Migrate()作为测试设置的一部分运行,然后进行播种,然后进行断言。然而,这只适用于测试给定最新架构的数据访问层(已应用所有迁移)。它对于测试特定迁移对已存在数据的影响没有用处。
问题:
其他人如何解决跨迁移测试数据完整性的问题?我正在寻找一种能够与 CI 管道配合良好的设置。
integration-testing asp.net-web-api ef-core-2.0 entity-framework-migrations
所以我用这样的用户和角色来播种我的数据库。
public static void SeedUsers(this ModelBuilder modelBuilder)
{
var roles = new[]
{
new Role
{
Id = new Guid("5127599a-e956-4f5e-9385-1b8c6a74e4f1"),
RoleName = "Customer"
},
new Role
{
Id = new Guid("8634c476-20fa-4391-b8f7-8713abf61af0"),
RoleName = "Admin"
}
};
// customer password
byte[] customerPasswordHash = null;
byte[] customerPasswordSalt = null;
HashPassword("asd123", out customerPasswordHash, out customerPasswordSalt);
// admin password
byte[] adminPasswordHash = null;
byte[] adminPasswordSalt = null;
HashPassword("asd123", out adminPasswordHash, out adminPasswordSalt);
var users = new[]
{
new Users()
{
Id = new Guid("3b86f5a2-1978-46e3-a0b6-edbb6b558efc"), …Run Code Online (Sandbox Code Playgroud) c# entity-framework-core .net-core ef-core-2.0 entity-framework-migrations
我一直在研究适合这个问题的多个问题、教程和示例。
如果我在创建第一个初始迁移时不知道我的连接字符串怎么办?鉴于我有机会在实例化上下文时设置连接字符串,例如:
var connection = @"Server=(localdb)\mssqllocaldb;Database=JobsLedgerDB;Trusted_Connection=True;ConnectRetryCount=0";
var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();
optionsBuilder.UseSqlServer(connection);
using (var context = new BloggingContext(optionsBuilder.Options))
{
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
如文档中所述..
如果您需要一个连接字符串来运行迁移,那么对于那些没有连接字符串的情况(从用户帐户获取连接字符串的租户数据库),您如何运行初始迁移?
您是否创建了一个虚拟连接字符串来创建迁移.. 对我来说似乎很狡猾。我希望对此有一些意见。
c# entity-framework entity-framework-core asp.net-core entity-framework-migrations
我是实体框架的新手。我见过使用 migrate.exe 等的示例enable-migration。
但 migrate.exe 不再存在。我浏览了网络,发现 ef6.exe 取代了旧的 migrate.exe
所以我尝试了
ef6 -contexttypename musicstoredatacontext
ef6 -context musicstoredatacontext
Run Code Online (Sandbox Code Playgroud)
没有任何作用
我找不到有关迁移命令的进一步支持/文档。我是移民新手。
有人可以给我一些提示如何处理它吗?
我本打算在启动时调用此方法,但它不在任何地方引用:
dbContext.Database.Migrate();
Run Code Online (Sandbox Code Playgroud)
类型
DatabaseFacade不包含 的定义,Migrate并且找不到Migrate类型的扩展方法DatabaseFacade(您是否缺少 using 指令或程序集引用?
那么我缺少哪个使用/程序集?
在 launchSettings.json 中,定义了一些变量并使用这些变量进行应用程序。当我正常运行应用程序时,launchSettings.json 中可用的环境变量成功加载,并且应用程序完美运行。但是当我尝试添加迁移时,ef 工具会忽略 launchSettings.json 文件。
launchSettings.json 看起来像
{“MIN_LOG_LEVEL”:“警告”}
将 launchSettings.json 变量读取为的代码
config.AddEnvironmentVariables();
string logLevel = config["MIN_LOG_LEVEL"] // returing null
Run Code Online (Sandbox Code Playgroud)
一些代码快照
更新:我尝试使用 IIS 和 Kestrel Server,但在这两种情况下都会出现此问题。
尝试从 Entity Framework 6.2 代码编写迁移脚本,但当显式指定源迁移和目标迁移时,向下迁移脚本生成失败。
例如,我有一个DbContext包含三个迁移的测试项目:
201506161504528_InitialCreate202202102238227_Add_Entity_FantasticalCreature202202102239481_FantasticalCreature_Add_IsWinged_Add_HasTail使用以下命令明确编写向上迁移脚本可以正常工作:
Update-Database -SourceMigration "201506161504528_InitialCreate" `
-TargetMigration "202202102238227_Add_Entity_FantasticalCreature" `
-Script -Verbose
.\migrate.exe "TestContext.dll"
/targetMigration="202202102238227_Add_Entity_FantasticalCreature"
/scriptFile="script.sql"
/sourceMigration="201506161504528_InitialCreate" /verbose
Run Code Online (Sandbox Code Playgroud)
此外,在显式指定源迁移和目标迁移时,使用具有类似参数的MigratorScriptingDecorator和ScriptUpdate也会生成向上脚本。
然而,当尝试执行相反的操作时,即编写向下迁移脚本,其中目标和源均已明确指定,并且源是“较低”MigrationId,则上述所有方法都将失败。
例如,Update-Database这样调用:
update-database -SourceMigration "202202102238227_Add_Entity_FantasticalCreature" `
-TargetMigration "201506161504528_InitialCreate" `
-Script -Verbose
Run Code Online (Sandbox Code Playgroud)
结果出现异常:
System.Data.Entity.Migrations.Infrastruct.MigrationsException:不支持在两个指定迁移之间编写降级脚本。
在 System.Data.Entity.Migrations.Infrastruct.MigratorScriptingDecorator.ScriptUpdate(String sourceMigration, String targetMigration)
在 System.Data.Entity.Migrations.Design.ToolingFacade.ScriptUpdateRunner.RunCore()
在 System.Data.Entity.Migrations.Design。 ToolingFacade.BaseRunner.Run()不支持在两个指定迁移之间编写降级脚本。
尝试使用 或 进行显式向下脚本迁移时也会发生相同的migrate.exe异常MigratorScriptingDecorator.ScriptUpdate。
但是,如果未显式指定源迁移,但指定了目标迁移,并且向比指定目标迁移“更高”迁移的数据库提供连接字符串,则上述所有三种方法都将成功生成向下迁移SQL脚本。
例如,使用以下命令成功生成向下迁移Update-Database:
# Assumes TestContextDb-01 is currently at migration 2 or 3. …Run Code Online (Sandbox Code Playgroud) c# entity-framework entity-framework-6 entity-framework-migrations
最近从 EF Core 1.0 迁移到 EF Core 2.0,并且运行良好。今天我添加了一个新表(代码优先)并将其添加到我的 DbContext 中:
public virtual DbSet<Foo> Foos { get; set; }
Run Code Online (Sandbox Code Playgroud)
当我运行迁移时,我的 DbContext 在一个单独的项目中(请记住,这之前都是有效的),迁移结束,但没有创建迁移文件。这一切都在 Core 2.0 之前使用:
http://paultechguy.blogspot.com/2017/04/entity-framework-core-migrating-and.html
PM> Add-Migration GradePremade -project Mfc.MfcRepositoryModel -verbose -context MfcDbContext -StartupProject web.xyz
Using project 'class.xyz\Mfc.MfcRepositoryModel'.
Using startup project 'web.xyz'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\web.xyz.deps.json --additionalprobingpath C:\Users\pcarver\.nuget\packages --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\web.xyz.runtimeconfig.json "C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.entityframeworkcore.tools\2.0.0\tools\netcoreapp2.0\ef.dll" migrations add GradePremade --json --context MfcDbContext --verbose --no-color --prefix-output --assembly C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\Mfc.MfcRepositoryModel.dll --startup-assembly C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\web.xyz.dll --project-dir C:\development\xyz\class.xyz\Mfc.MfcRepositoryModel --root-namespace Mfc.MfcRepositoryModel
Using assembly 'Mfc.MfcRepositoryModel'. …Run Code Online (Sandbox Code Playgroud) c# entity-framework database-migration entity-framework-core entity-framework-migrations
我正在使用 .NET Core 3.1 和 EntityFramework Core 3.1.3。我正在尝试使用 DB 模式实现租户数据分离。我读了这个。我知道它有点过时了,所以我已经调整了。
我已经创建了一个 DbContext 的实现:
public class AppDataContext : DbContext
{
private readonly ITenantProvider _tenantProvider;
public AppDataContext(DbContextOptions<AppDataContext> options, ITenantProvider tenantProvider) : base(options)
{
_tenantProvider = tenantProvider;
}
public DbSet<Book> Books { get; set; }
public DbSet<Comics> Comics { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Tenant schema mapping
var tenant = _tenantProvider.GetTenantString();
modelBuilder.HasDefaultSchema(tenant);
}
public static void ApplyMigrations(string connectionString, string tenant)
{
var optionsBuilder = new DbContextOptionsBuilder<AppDataContext>(); …Run Code Online (Sandbox Code Playgroud) c# sql-server entity-framework-core .net-core entity-framework-migrations
entity-framework-migrations ×10
c# ×6
.net-core ×2
ef-core-2.0 ×2
asp.net-core ×1
dbcontext ×1
ef-core-3.0 ×1
sql-server ×1