3

我正在尝试自定义 MVCScaffolding 中使用的 T4 模板。我想过滤掉具有该属性的NotMapped属性。

我的问题是属性集合似乎是空的。我尝试打印出如下属性:

List<ModelProperty> properties = GetModelProperties(Model.ViewDataType, true);
foreach (ModelProperty property in properties) {
    if (!property.IsPrimaryKey && !property.IsForeignKey) {
#>
        <th>
            @Html.LabelFor(x => x.First().<#= property.Name #>)
            <!--
            <#foreach(var attribute in property.Type.CodeType.Attributes)
            {#>
              Attribute: <#=attribute.GetType().Name#>
            <#}#>
            -->
        </th>
<#
    }
}
#>

我找不到有关此实际有效的任何信息,到目前为止,这是一个非常缓慢的试验和错误案例。获取属性或让模板忽略未映射属性的正确方法是什么?

4

1 回答 1

1

这不是您问题的确切答案,但它将提供有关该问题的更多信息。我的经历完全一样。这个问题有一些奇怪的地方:

  • 它以前工作过。我运行与 2 个月前相同的代码(它来自源代码管理,所以我敢肯定),今天它失败了。
  • 从 T4 引擎(例如 MVC 应用程序)外部运行时,相同的代码工作得非常好。

我已经更改了我的代码,以便它可以从 Visual Studio 外部运行:

string assemblyPath = Host.ResolveAssemblyReference("$(ProjectDir)$(OutDir)T4Mvc.dll");

改为:

string assemblyPath = @"D:\AbsolutePath\bin\T4Mvc.dll";

然后我用 TextTransform.exe 代替 RMB > Run Custom Tool

利用

TextTransform.exe AjaxServices.tt -out AjaxServices.js

(您可以在以下位置找到 TextTransform.exe:"C:\Program Files (x86)\Common Files\microsoft shared\TextTemplating\10.0")。

我也禁用了特定于主机的标志。

从 TextTransform.exe 而不是 VS 执行时,相同的模板效果很好。似乎是VS的某种问题。请尝试使用 TextTransform。exe并告诉它是否有效。

于 2012-01-05T00:54:46.863 回答