0

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!

joelnet
  • 13,621
  • 5
  • 35
  • 49

2 Answers2

0

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)#>;
  • 1
    Ok, this results in "NorthwindModel", which is listed as the Namespace in the Properties dialog, but I need the actual namespace the Data Context and Models are in. In my example it should be "MyProject.Entities". – joelnet May 16 '12 at 21:18
0

This is old, but since I stumbled upon it looking for an answer, I'll provide what I found... the properties of the *.tt includes an option called "Custom Tool Namespace". This will override the use of the MetadataLoader which looks at the phyical structure to determine the namespace. I had to set this for both the .Context.tt and .tt files for my old database first project.

Credit to this responses here that put me on the right path.

LoJo
  • 142
  • 2
  • 11