获取 T4 模板中 edmx 的命名空间

发布于 2024-12-28 22:36:16 字数 750 浏览 4 评论 0原文

当我将模板安装到与 edmx 命名空间不同的命名空间中时,生成的代码由于缺少命名空间而出现错误。

所以我试图添加一个“using”语句来解决这个问题,但我不知道如何获取 edmx 所在的命名空间。

到目前为止我已经做到了(为简洁起见进行了编辑):

<#@ 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 #>
{
    // ...
}

请帮我解决“ edmx命名空间名称”。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

您可以使用 MetadataLoader 类

<#
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)#>;

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)#>;
生生不灭 2025-01-04 22:36:16

这是旧的,但由于我偶然发现它寻找答案,我将提供我发现的内容...... *.tt 的属性包括一个名为“自定义工具命名空间”的选项。这将覆盖 MetadataLoader 的使用,MetadataLoader 查看物理结构以确定命名空间。我必须为我的旧数据库第一个项目的 .Context.tt 和 .tt 文件设置此项。

感谢这里的回复,让我走上了正确的道路。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文