我在这里获取实体名称,但是我无法在调试器中看到元素名称及其数据类型,如下图所示。
调试器图像显示了以“ entity.Type.ElementType”开头的元素数据类型的路径,但是Intellisense显示了“ ElementType”不是一个选项,尽管我在调试器图像中看到了它。
我看到数据类型的路径是变量实体->类型->元素类型->定义->声明的属性->类型->定义->名称,但智能感知将无助于达到这一水平。
答案 0 :(得分:0)
这是最终有效的方法:
var request = WebRequest.CreateHttp(baseUrl + "$metadata");
var settings = new ODataMessageReaderSettings() { MessageQuotas = new ODataMessageQuotas() { MaxReceivedMessageSize = 35000000 } };
var metadataMessage = new ClientHttpResponseMessage((HttpWebResponse)request.GetResponse());
using (var messageReader = new ODataMessageReader(metadataMessage, settings))
{
IEdmModel edmModel = messageReader.ReadMetadataDocument();
foreach (var entity in edmModel.EntityContainer.EntitySets())
{
string entityName = entity.Name; // entity name
IEdmCollectionType edmCollectionType = (IEdmCollectionType)entity.Type;
IEdmType edmType = edmCollectionType.ElementType.Definition;
IEdmStructuredType edmStructuredType = edmType as IEdmStructuredType;
foreach (IEdmProperty property in edmStructuredType.DeclaredProperties)
{
string propertyName = property.Name;
IEdmType edmType2 = property.Type.Definition;
var primitive = edmType2 as IEdmPrimitiveType;
string elementName = primitive.Name;
}
}
}