Microsoft.VisualStudio.Modeling.DomainDataNotFoundException:在目录中找不到具有标识的域对象

Microsoft.VisualStudio.Modeling.DomainDataNotFoundException : Domain object with identity was not found in directory

我正在使用 Visual Studio 2015 建模 SDK 中的 DSL 工具开发 DSL。

DSL 工作正常。问题发生在我尝试编写一些单元测试以处理 DSL 生成的某些 classes 的地方。

我想创建 DSL 中包含的某些元素的实例,然后在其上测试 运行 一些代码。

我有一个 Feature class,它是 DSL 的一部分。它是自动生成的并继承自 ModelElement(来自建模 SDK)。我想创建它的一个实例并 运行 对其进行一些测试。它的构造函数需要一个 Store 对象(同样来自建模 SDK)。

我有以下内容:

using (var store = new Store())
using (var transaction = store.TransactionManager.BeginTransaction("create model"))
{
    var rootFeature = new Feature(store);
    // Do something with rootFeature...
}

然而这是投掷:

Microsoft.VisualStudio.Modeling.DomainDataNotFoundException : Domain object with identity DSL.Feature was not found in directory.

怎么会?

您创建商店的方式有问题。您需要传入域模型 class,如下例所示:

    [TestMethod]
    public void TestMethod1()
    {
        using (Store store = new Store(typeof(EntitiesModel3DomainModel)))
        {
            using (Transaction trans = store.TransactionManager.BeginTransaction())
            {
                Entity entity = new Entity(store);
                Assert.IsNotNull(entity);
            }
        }
    }

这里的 EntitiesModel3DomainModel 是我拥有的 DSL 的领域模型。 您需要将其替换为您自己的 class,由 DomainModel.tt.

生成