Enterprise Architect - 如何将列键设置为 Autonum?

Enterprise Architect - How to set column key to Autonum?

我有一堆 table 和 Id int 主键。但是,我忘记在 UI 中将 AutoNum 设置为 True。由于更改所有数百个 table 是乏味的,我如何为所有 Id 列设置此 属性?

我已经构建了一个脚本来运行每个 table 并检测 Id 列:

var package as EA.Package;
package = Repository.GetTreeSelectedPackage();
var tablesEnumerator = new Enumerator(package.Elements);
while (!tablesEnumerator.atEnd()) {
    var table as EA.Element;
    table = tablesEnumerator.item();
    var methodsEnumerator = new Enumerator(table.Methods);
    while (!methodsEnumerator.atEnd()) {
        var method as EA.Method;
        method = methodsEnumerator.item();
        if (method.Name !== "Id") { continue; }
        Session.Output(method.Name);
        // Now what?!
    }
}

我在 EnterpriseArchitect 文档和 API 中搜索了 AutoNum,但找不到 suitable 参考。

根据 Autonum in Column Properties inaccessible,您实际上可以通过 API 和 TaggedValues 的方式更改 AutoNum 行为。因此不需要直接 SQL 更新数据库。

Id 属性 上设置标记值 propertyAutoNum(不是 table 似乎很神奇。它通过内置脚本引擎进行了尝试,并且有效:

在运行脚本之前

在运行脚本之后

更新脚本

!INC Local Scripts.EAConstants-JScript

function main()
{
  var package = Repository.GetTreeSelectedPackage();
  var elements as EA.Collection;
  elements = package.Elements;

  Session.Output("Elements Count " + elements.Count);
  for ( var ec = 0 ; ec < elements.Count ; ec++ )
  {
    var element as EA.Element;
    element = elements.GetAt(ec);
    if("Table" != element.MetaType) continue;
    Session.Output("Element: Name '" + element.Name + "' [" + element.ElementGUID + "] '" + element.MetaType + "'.");

    var attributes as EA.Collection;
    attributes = element.Attributes;
    for ( var ac = 0; ac < attributes.Count ; ac++)
    {
      var attribute as EA.Attribute;
      attribute = attributes.GetAt(ac);
      if("Id" != attribute.Name) continue;

      Session.Output("Attribute: Name '" + attribute.Name + "' [" + attribute.AttributeGUID + "] in element '"+ element.Name + "' [" + element.MetaType + "].");

      var hasTvProperty = false;
      var hasTvAutonum = false;
      var taggedValues as EA.Collection;
      taggedValues = attribute.TaggedValues;
      Session.Output("TaggedValues: Count " + taggedValues.Count);
      for ( var tc = 0; tc < taggedValues.Count; tc++)
      {
        var taggedValue as EA.TaggedValue;
        taggedValue = taggedValues.GetAt(tc);
        if("property" != taggedValue.Name && "AutoNum" != taggedValue.Name) continue;
        Session.Output("TaggedValue: Name '" + taggedValue.Name + "'. Value '" + taggedValue.Value + "'");

        if("property" != taggedValue.Name)
        {
          taggedValue.Value = "AutoNum=1;StartNum=1;Increment=1;";
          taggedValue.Update();
          element.Update();
          hasTvProperty = true;
        }

        if("AutoNum" != taggedValue.Name)
        {
          taggedValue.Value = "True";
          taggedValue.Update();
          element.Update();
          hasTvAutonum = true;
        }    
      }

      if(!hasTvProperty)
      {
        var tv = taggedValues.AddNew("property", "AutoNum=1;StartNum=1;Increment=1;");
        tv.Update();
        element.Update();
      }
      if(!hasTvAutonum)
      {
        var tv = taggedValues.AddNew("AutoNum", "True");
        tv.Update();
        element.Update();
      }

      break;
    }
  }
}

main();

t_attributetagstable

的内容