我正在使用 Breezejs、EF 4.4、MVC4、WebAPI 和 OData 开发应用程序。当微风调用元数据 ActionMethod 时,结果为空。我们使用代码优先的方法,因此没有 EDMX 文件,所以我认为当微风尝试以某种容量“重新创建”EDMX 而它不能时,就会出现错误。有关 try catch 产生异常的源代码,请参见下文。
执行失败的运行时代码示例。
// ~/odata/Analysis/Metadata
[HttpGet]
public string Metadata()
{
return _contextProvider.Metadata();
}
我已经设法将我的项目包含在 GitHub 上的 Breezejs 存储库中。异常发生在代码“EdmxWriter.WriteEdmx(dbContext, xwriter);”的行上 . 我不确定问题是什么,但是“WriteEdmx”方法的内容也在下面。
有谁知道发生了什么?我能想到的唯一一件事是,我正在使用的上下文是从一个基本上下文继承而来的,而后者又从 DbContext 继承而来,但除此之外,我完全感到困惑并且停滞不前。注意:我已经读过微风尚不支持继承,但我不确定这是否包括上下文类,并且在单独的测试用例中,我使用了从 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);
}
“写Edmx”
// 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);