Getting the namespace for an edmx in a T4 template

蓝咒 提交于 2020-01-07 05:29:11

问题


When I install my template into a different namespace than the edmx's namespace the code that is generated has errors because of the missing namespace.

So I'm trying to add a "using" statement to solve this problem, but I don't know how to get the namespace the edmx is in.

I have this so far (edited for brevity):

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

const string edmxFile = @"../Entities/NorthwindEntities.edmx";

CodeGenerationTools code = new CodeGenerationTools(this);

string namespaceName = code.VsNamespaceSuggestion();
string edmxNamespaceName = "???";

using System;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using <#= edmxNamespaceName #>;

namespace <#= namespaceName #>
{
    // ...
}

please help me solve for "edmxNamespaceName".

Thanks!


回答1:


You can use the MetadataLoader class

<#
string edmxFile = @"../Entities/NorthwindEntities.edmx";
CodeGenerationTools code = new CodeGenerationTools(this);

MetadataLoader loader = new MetadataLoader(this);
string modelNamespace = loader.GetModelNamespace(edmxFile);
#>

using System;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using <#=code.Escape(modelNamespace)#>;


来源:https://stackoverflow.com/questions/8997442/getting-the-namespace-for-an-edmx-in-a-t4-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!