问题
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