【问题标题】:The type or namespace name Edm could not be found are you missing a using directive or an assembly reference找不到类型或命名空间名称 Edm 是否缺少 using 指令或程序集引用
【发布时间】:2014-12-15 12:29:52
【问题描述】:

全部:

关于用于开发 ASP.NET 应用程序的技术环境的详细信息:

  • .NET Framework 4.5
  • 带有更新 4 的 Microsoft Visual Studio 2012 Ultimate
  • System.Web.Http.OData(版本 5.3.1.0)作为 Visual Studio 中引用的 .NET 程序集
  • System.Web.OData(版本 5.3.1.0)作为 Visual Studio 中引用的 .NET 程序集
  • Microsoft.OData.Edm(版本 6.8.1.0)作为 Visual Studio 中引用的 .NET 程序集

我们有一个类,其中包含以下删除方法:

public IHttpActionResult Delete([FromODataUri] Edm.Guid id){...}

遗憾的是,上述方法 Edm.Guid 抱怨以下错误:

找不到类型或命名空间名称“Edm”(您是否缺少 using 指令或程序集引用?)

我无法删除 Edm,因为上述代码是使用 T4 模板生成的,删除 Edm 会很痛苦。

无论如何,能否请一些人告诉我上述代码将如何工作以使 Edm 被识别?

--更新 我找到了一个我不喜欢的非常规解决方案,因为它有点简单,意味着它就像一个 hack。

在我的 T4 模板中,我有以下内容:

    var keyParameters = string.Join(", ", keys[entityName].Select(key => "[FromODataUri] " + (key.TypeUsage.ToString()).Substring((key.TypeUsage.ToString()).IndexOf(".") + 1).Trim()  + " " + code.Escape(key.Name).ToLower()));

...........
........
.....
public IHttpActionResult Delete(<#= keyParameters #>) {
...........
........
.....

这很简单,因为我正在做奇怪的字符串操作来摆脱任何以“Edm”为前缀的东西。 :

   (key.TypeUsage.ToString()).Substring((key.TypeUsage.ToString()).IndexOf(".")

请随时提出更好的替代解决方案。

【问题讨论】:

  • 不确定为什么需要使用“Edm.Guid”?纯 C# 中的 Guid 应该可以工作。

标签: visual-studio-2012 odata .net-assembly t4 ado.net-entity-data-model


【解决方案1】:

您似乎正在尝试解析 OData 类型名称。这些是well-knownusually parsed with a map 从名称到类型,而不是通过字符串操作。类似以下的方法会起作用:

var typeMap = new Dictionary<string, Type> 
{
    { "Edm.Binary", typeof(Byte[]) },
    { "Edm.Boolean", typeof(Boolean) },
    { "Edm.Byte", typeof(Byte) },
    { "Edm.DateTime", typeof(DateTime) },
    { "Edm.Decimal", typeof(Decimal) },
    { "Edm.Double", typeof(Double) },
    { "Edm.Single", typeof(Single) },
    { "Edm.Guid", typeof(Guid) },
    { "Edm.Int16", typeof(Int16) },
    { "Edm.Int32", typeof(Int32) },
    { "Edm.Int64", typeof(Int64) },
    { "Edm.SByte", typeof(SByte) },
    { "Edm.String", typeof(String) },
    { "Edm.Time", typeof(TimeSpan) },
    { "Edm.DateTimeOffset", typeof(DateTimeOffset) },
}

var edmTypeName = "Edm.Guid";
var typeName = "global::" + typeMap.SingleOrDefault(t => t.Key.Equals(edmTypeName, StringComparison.CurrentCultureIgnoreCase)).Value.FullName;
Debug.Assert(typeName == "global::System.Guid");

话虽如此,您可以通过给 System 命名空间起别名来变得聪明。将using Edm = System; 添加到模板顶部对大多数原始类型都有效,但这是一种 hack。

【讨论】:

    猜你喜欢
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 2017-02-14
    • 2012-05-01
    • 2020-11-28
    • 2015-06-12
    • 2015-04-19
    相关资源
    最近更新 更多