c# - 由于关键字为 "using"的未知命名空间,运行转换失败

标签 c# entity-framework t4 edmx csdl

我想在我的 *.csdl 中使用“Using”元素来导入其他命名空间,并使用 POCO 来转换对象。

我使用的 CSDL 看起来像这样:

<Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm"
          xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration"
          xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator"
          Namespace="BooksModel" Alias="Self">

    <Using Namespace="BooksModel.Extended" Alias="BMExt" />

    <EntityContainer Name="BooksContainer" >
      <EntitySet Name="Publishers" EntityType="BooksModel.Publisher" />
    </EntityContainer>

    <EntityType Name="Publisher">
      <Key>
        <PropertyRef Name="Id" />
      </Key>
      <Property Type="Int32" Name="Id" Nullable="false" />
      <Property Type="String" Name="Name" Nullable="false" />
      <Property Type="BMExt.Address" Name="Address" Nullable="false" />
    </EntityType>

</Schema>

(http://msdn.microsoft.com/en-us/library/bb738545.aspx)

但是,当我使用模板 (POCO) 来转换我的 CSDL 时,运行工具会抛出转换错误:

Running transformation: No schema encountered with 'BooksModel.Extended' namespace. Make sure the namespace is correct or the schema defining the namespace is specified.

Running transformation: Unknown namespace or alias (BooksModel.Extended).

我像这样加载我的 CSDL:

var inputFile = @"CSDL_NAME.csdl";
var ItemCollection = loader.CreateEdmItemCollection(inputFile);

如何修改模板以包含未知的命名空间?

最佳答案

错误背后的问题是您没有加载 EdmItemCollection 中的其他 CSDL 文件。解决方案是将包含必要 CSDL 文件(包括具有导入命名空间的文件)的路径的 String[] 加载到 EdmItemCollection。

在代码中,它看起来像这样:

List<string> lstCsdlPaths = new List<string>();
lstCsdlPaths.Add(@"path\CSDLBase.csdl");
lstCsdlPaths.Add(@"path\CSDLImports.csdl");
var ItemCollection = new EdmItemCollection(lstCsdlPaths.ToArray()); 

关于c# - 由于关键字为 "using"的未知命名空间,运行转换失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10224104/

相关文章:

C# async/await - 多项任务首选一项

c# - 运行 Web 角色(Web 应用程序)的 Azure 云服务内存问题(Gen2)

t4 - 从 t4 模板编译 typescript 文件

c# - 为什么我无法访问 T4 模板中类成员中的实用方法?

c# - 如何使用 C# 向 facebook 收件箱发送消息?

c# - 通过 WCF 双工 channel 通过 SocketIO4NET 使用 Socket.IO 时应用程序关闭

c# - 如何从数据库中获取 SQL CLR 函数的定义

entity-framework - Entity Framework 5 SaveChanges 上的外键新记录

entity-framework - EF4 : Splitting a table to two entities using POCOs - why am I getting an exception?

t4 - VS2012 中未找到的 EnvDTE 在 VS2010 中有效