c# - T4 模板生成的文件不包含在项目中

标签 c# asp.net-mvc t4

我正在使用 T4 模板(运行时生成的)为我的项目( Controller 、实体和 View )生成几个 .cs 文件,这些文件需要通过 Controller 的操作动态创建, Controller 的操作将特定数据传递给这些模板。

这是我在操作中生成它们的方式:

 CustomEntity entity = new CustomEntity(entityName, propertyMap);
 String entityContent = entity.TransformText();
 System.IO.File.WriteAllText(Server.MapPath("~") + "\\Models\\" + entityName + ".cs", entityContent);

 CustomEntityController controller = new CustomEntityController(entityName);
 String controllerContent = controller.TransformText();
 System.IO.File.WriteAllText(Server.MapPath("~") + "\\Controllers\\" + entityName + "Controller.cs", controllerContent);

它们是正确生成的,但是它们没有包含在我的项目中,所以我无论如何都不能在我的项目中使用它们。只有右键单击文件并“包含在项目中”才能解决此问题,但我需要在我的项目中自动访问它们。

我该如何解决这个问题?

最佳答案

您可以使用递归通配符包含将动态生成的 .cs 文件包含到 Visual Studio 项目中,如 this answer 中所述.这可以通过手动编辑 csproj 文件来完成。

语法如下(根据您的情况检查正确的路径):

<ItemGroup>
  <Compile Include="Models\**\*.cs" />
  <Compile Include="Controllers\**\*.cs" />
</ItemGroup>

关于c# - T4 模板生成的文件不包含在项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48039049/

相关文章:

c# - T4 模板使用来自服务器资源管理器的连接

c# - 在文本框中打断单词(使用 TextWrapping=Wrap)

c# - WCF 在调用期间等待 _TransparantProxyStub_CrossContext 函数时使 CPU 最大化

c# - 从单页中删除布局

c# - SQL - ASP.NET Core 2.1.7 中的故意注入(inject)

visual-studio - 使用 T4 获取项目或相关目录

c# - AspCore T4 生成添加对 System.Runtime.DLL 的引用

c# - 使用 C# 检查 Windows 7 上是否启用了 IPv6

c# - char 160 在我的源代码中意味着什么?

c# - 如何在嵌套模型中访问 DisplayNameFor