Breeze Metadata在GetMetadataFromDbContext方法上返回null - EF 4.4 MVC4 WebAPI OData

时间:2013-06-25 21:09:22

标签: breeze

我正在使用Breezejs,EF 4.4,MVC4,WebAPI和OData开发应用程序。当breeze调用Metadata ActionMethod时,结果为null。我们使用代码优先方法,因此没有EDMX文件,所以 我认为当微风尝试以某种身份“重新创建”EDMX时它就会出现错误。请参阅以下有关try catch产生异常的源代码

执行失败的运行时代码示例。

// ~/odata/Analysis/Metadata 
[HttpGet]
public string Metadata()
{
  return _contextProvider.Metadata();
}

我已经设法将我的项目包含在位于GitHub上的Breezejs存储库中。代码“EdmxWriter.WriteEdmx(dbContext,xwriter);”的行发生异常。我不确定问题是什么,但“WriteEdmx”方法的内容也在下面。

有谁知道发生了什么事?我能想到的唯一一件事就是我使用的上下文是从一个基本上下文继承的,然后继承自DbContext,但除此之外,我完全感到困惑并停滞不前。注意:我已经读过在breeze中尚未支持继承,但我不确定是否包含了contexts类,在单独的测试用例中我使用了从DbContext继承的上下文,但仍然收到了相同的错误。

private static String GetMetadataFromDbContext(Object context) {
  var dbContext = (DbContext) context;
  XElement xele;

  try {
    using (var swriter = new StringWriter()) {
      using (var xwriter = new XmlTextWriter(swriter)) {
        EdmxWriter.WriteEdmx(dbContext, xwriter);
        xele = XElement.Parse(swriter.ToString());
      }
    }
  } catch (Exception e) {
    if (e is NotSupportedException) {
      // DbContext that fails on WriteEdmx is likely a DataBase first DbContext.
      return GetMetadataFromObjectContext(dbContext);
    } else {
      throw;
    }
  }

  var ns = xele.Name.Namespace;
  var conceptualEle = xele.Descendants(ns + "ConceptualModels").First();
  var schemaEle = conceptualEle.Elements().First(ele => ele.Name.LocalName == "Schema");
  var xDoc = XDocument.Load(schemaEle.CreateReader());

  var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
  // This is needed because the raw edmx has a different namespace than the CLR types that it references.
  xDoc = UpdateCSpaceOSpaceMapping(xDoc, objectContext);
  return XDocToJson(xDoc);
}

“WriteEdmx”

// Summary:
//     Uses Code First with the given context and writes the resulting Entity Data
//     Model to the given writer in EDMX form. This method can only be used with
//     context instances that use Code First and create the model internally. The
//     method cannot be used for contexts created using Database First or Model
//     First, for contexts created using a pre-existing System.Data.Objects.ObjectContext,
//     or for contexts created using a pre-existing System.Data.Entity.Infrastructure.DbCompiledModel.
//
// Parameters:
//   context:
//     The context.
//
//   writer:
//     The writer.
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Edmx")]
public static void WriteEdmx(DbContext context, XmlWriter writer);

1 个答案:

答案 0 :(得分:0)

更新:从EF 4.4降级到EF 4.1似乎解决了这个问题。升级到EF 5.0或每晚构建也可能会这样做。

对于这个不起眼的问题,这是我能做的最好的事情。的 #baffled