如何使用PetaPOCO(Umbraco 6,MVC)修改现有数据库表(添加/删除列)

Dan*_*ulp 1 c# sql umbraco petapoco umbraco6

我有一个具有一些自定义功能的Umbraco CMS应用程序,为此我使用了PetaPOCO将数据存储在数据库中。我创建了POCO和一个Umbraco事件,该事件在应用程序启动时触发以创建表(如果该表不存在):

public class RegisterEvents : ApplicationEventHandler
{
    //This happens everytime the Umbraco Application starts
    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        //Get the Umbraco Database context
        var db = applicationContext.DatabaseContext.Database;

        //Check if the DB table does NOT exist
        if (!db.TableExist("MyTable"))
        {
            //Create DB table - and set overwrite to false
            db.CreateTable<MyPetaPOCO>(false);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如何在不直接访问数据库的情况下修改现有数据库(我想添加一列)?我需要使用代码,因为主机尚未提供访问权限。我认为我应该能够在此ApplicationStarted重写事件中执行此操作,但我不知道如何操作。

编辑

我应该使用Somel Fluent Migrator

小智 5

如果这是一个程序包或正在部署(或正在消耗其他程序的东西),则应创建一个迁移并在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");
    }
  }
Run Code Online (Sandbox Code Playgroud)

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);
     }
   }
 }
Run Code Online (Sandbox Code Playgroud)

请注意,目标版本应与您在Migrationclass属性中设置的版本匹配(在此示例中为1.0.1)

在进行更新并向应用程序或插件添加新功能时,您将创建新的迁移(如果需要),并更新目标版本。