Model from ODataConventionModelBuilder different from ReadMetadataDocument
Followed code http://odata.github.io/odata.net/v7/ to re-build a model as below. But the new model is a CsdlSemanticsModel instead of an EdmModel, and the ODataPath object will consist of CSDL segments instead of EntitySet segments. How to convert CsdlSemanticsModel to EdmModel?
IEdmModel edmModel = (new ODataConventionModelBuilder()).GetEdmModel(); MemoryStream stream = new MemoryStream(); InMemoryMessage message = new InMemoryMessage { Stream = stream }; ODataMessageWriterSettings writerSettings = new ODataMessageWriterSettings(); ODataMessageWriter writer = new ODataMessageWriter((IODataResponseMessage)message, writerSettings, model); writer.WriteMetadataDocument(); stream.Position = 0; ODataMessageReaderSettings settings = new ODataMessageReaderSettings(); ODataMessageReader reader = new ODataMessageReader((IODataResponseMessage)message, settings); IEdmModel modelFromReader = reader.ReadMetadataDocument();
@johnhzhu
CsdlSemanticsModel inherits from EdmModelBase and implements IEdmCheckable interface.
EdmModelBase inherits from EdmElement and implements IEdmModel interface.
We can cast the CsdlSemanticsModel to IEdmModel but not EdmModel.
var semanticModel = new CsdlSemanticsModel(csdlModel, new EdmDirectValueAnnotationsManager(), Enumerable.Empty<IEdmModel>());
var edmModel = semanticModel as IEdmModel;
@johnhzhu IEdmModel is the general interface for models. The library provides 2 main implementations: EdmModel and CsdlSemanticsModel.
EdmModel is used to manually construct a model in-memory. This is what the model builder uses internally. The CsdlSemanticsModel is internal and is used as the result of parsing CSDL file using the CsdlReader.
You cannot cast a CsdlSemanticsModel to EdmModel as there are two different classes and one is not a subclass of the other. Their internal implementations and structures are very different.
May ask, what is your use case and what you're trying to achieve.
Is it that you want to parse a model from file, then modify it in-memory? If that's the case, that feature is not provided out of the box by the library. You would need to manually copy elements over from the read IEdmModel to the EdmModel. We're currently looking into providing an easier way of modifying parsed models out of the box.
Question has been answered