如何修复Entity Framework 4.1模型模板中缺少的类型/命名空间错误?

时间:2011-06-23 19:07:17

标签: visual-studio-2010 entity-framework code-generation compiler-errors t4

我有一个T4模板,可以为支持.edmx模型的实体生成C#类。模板以此标头开头:

<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ output extension=".cs"#>

尝试构建项目会导致这些错误

error CS0234: The type or namespace name 'Design' does not exist in the namespace 'System.Data.Entity'...
error CS0246: The type or namespace name 'EnvDTE' could not be found (are you missing a using directive...
error CS0234: The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft'...

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:4)

事实证明,基于VS Extensibility thread,问题的原因是 Clarius Visual T4扩展。它在.csproj中重置此节点 归档

<Compile Include="SomeModel.tt">
  <Generator>TextTemplatingFileGenerator</Generator>
  <LastGenOutput>SomeModel.cs</LastGenOutput>
</Compile>

<None Include="SomeModel.tt">
  <Generator>TextTemplatingFileGenerator</Generator>
  <LastGenOutput>SomeModel.cs</LastGenOutput>
</None>

解决方案是手动将节点更改为None文件中的.csproj。改变它 通过Visual Studio属性编辑器返回.tt文件不起作用。 最后,禁用扩展可以防止再次发生这种情况。

答案 1 :(得分:0)

只需在.tt文件的开头添加所需的程序集即可;像这样:

<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ assembly name="System.Data.Entity" #>
<#@ assembly name="System.Data.Entity.Design" #>
...