
[英]How to force the migration to use connection string of the client context triggered the migration
[英]How to pass context to migration?
我想序列化一个表中的数据并将其作为迁移的一部分放入另一个表中。 问题是我正在使用服务器上不存在的本地机密创建上下文。 如何将上下文转发给迁移,以免在迁移中创建上下文。
private static IConfigurationRoot Configuration { get; set; }
protected override void Up(MigrationBuilder migrationBuilder)
{
//initialization
var orgStructureAppNodes = new List<BaseApplicationNode>();
var documentFlowAppNodes = new List<BaseApplicationNode>();
var jsonSettings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All,
PreserveReferencesHandling = PreserveReferencesHandling.Objects
};
//create context
var builder = new ConfigurationBuilder();
builder.AddUserSecrets<TbpmApplicationMigrateToNewNavigator>();
Configuration = builder.Build();
var options = new DbContextOptionsBuilder<ApplicationPostgreSqlContext>()
.UseNpgsql(migrationBuilder.ActiveProvider)
.Options;
var context = new ApplicationPostgreSqlContext(options);
context.Database.OpenConnection();
using (var command = context.Database.GetDbConnection().CreateCommand())
{
command.CommandText =
"SELECT \"Id\", \"Title\", \"Icon\", \"Type\", \"TargetId\", \"SectionId\" FROM \"navigator\".\"Nodes\"";
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
Guid entityId = Guid.Empty;
if (Guid.TryParse(reader.GetValue(4).ToString(), out var id))
entityId = id;
var app = new ApplicationEntityNode
{
Id = Guid.Parse(reader.GetValue(0).ToString()),
Title = reader.GetValue(1).ToString(),
Icon = reader.GetValue(2).ToString(),
Type = (ApplicationNodeType)int.Parse(reader.GetValue(3).ToString()),
EntityId = entityId
};
if (reader.GetValue(5).ToString() ==
"d6a9b28b-4dbe-4ef8-bb74-53e999723b04") //orgStructure sectionId
orgStructureAppNodes.Add(app);
else documentFlowAppNodes.Add(app);
}
}
}
//serialization and insert data
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.