これがパッケージまたは展開する (または他のユーザーに使用させる) ものである場合は、移行を作成し、ApplicationStarted メソッドで実行する必要があります。
https://cultiv.nl/blog/using-umbraco-migrations-to-deploy-changes/
上記の記事の例:
既存の PetaPOCO DB に列を追加するには:
MigrationBase から派生する移行クラスを作成します。
[Migration("1.0.1", 1, "YourTableName")]
public class AddNewColumnToTable : MigrationBase
{
public AddNewColumnToTable(ISqlSyntaxProvider sqlSyntax, ILogger logger)
: base(sqlSyntax, logger)
{ }
public override void Up()
{
Alter.Table("YourTableName").AddColumn("ColumnToAdd").AsString().Nullable();
}
public override void Down()
{
Delete.Column("ColumnToAdd").FromTable("YourTableName");
}
}
ロジックを更新ApplicationStarted
して移行を実行します。
public class MyApplication : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
HandleStatisticsMigration();
}
private static void HandleStatisticsMigration()
{
const string productName = "YourTableName";
var currentVersion = new SemVersion(0, 0, 0);
// get all migrations for "YourTableName" already executed
var migrations = ApplicationContext.Current.Services.MigrationEntryService.GetAll(productName);
// get the latest migration for "YourTableName" executed
var latestMigration = migrations.OrderByDescending(x => x.Version).FirstOrDefault();
if (latestMigration != null)
currentVersion = latestMigration.Version;
var targetVersion = new SemVersion(1, 0, 1);
if (targetVersion == currentVersion)
return;
var migrationsRunner = new MigrationRunner(
ApplicationContext.Current.Services.MigrationEntryService,
ApplicationContext.Current.ProfilingLogger.Logger,
currentVersion,
targetVersion,
productName);
try
{
migrationsRunner.Execute(UmbracoContext.Current.Application.DatabaseContext.Database);
}
catch (Exception e)
{
LogHelper.Error<MyApplication>("Error running YourTableName migration", e);
}
}
}
ターゲット バージョンは、Migration
class 属性で設定したバージョン (この例では 1.0.1)と一致する必要があることに注意してください。
アプリケーションまたはプラグインに更新を行って新しい機能を追加すると、(必要に応じて) 新しい移行を作成し、ターゲット バージョンを更新します。