/// <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 <see cref="T:System.Data.Entity.Core.Objects.ObjectContext" />, or /// for contexts created using a pre-existing <see cref="T:System.Data.Entity.Infrastructure.DbCompiledModel" />. /// </summary> /// <param name="context"> The context. </param> /// <param name="writer"> The writer. </param> public static void WriteEdmx(DbContext context, XmlWriter writer) { Check.NotNull <DbContext>(context, nameof(context)); Check.NotNull <XmlWriter>(writer, nameof(writer)); InternalContext internalContext = context.InternalContext; if (internalContext is EagerInternalContext) { throw Error.EdmxWriter_EdmxFromObjectContextNotSupported(); } DbModel beingInitialized = internalContext.ModelBeingInitialized; if (beingInitialized != null) { EdmxWriter.WriteEdmx(beingInitialized, writer); } else { DbCompiledModel codeFirstModel = internalContext.CodeFirstModel; if (codeFirstModel == null) { throw Error.EdmxWriter_EdmxFromModelFirstNotSupported(); } DbModelBuilder dbModelBuilder = codeFirstModel.CachedModelBuilder.Clone(); EdmxWriter.WriteEdmx(internalContext.ModelProviderInfo == null ? dbModelBuilder.Build(internalContext.Connection) : dbModelBuilder.Build(internalContext.ModelProviderInfo), writer); } }
/// <summary> /// Saves a model to the store. /// </summary> /// <param name="contextType">The type of context representing the model.</param> /// <param name="model">The metadata model to save.</param> public override void Save(Type contextType, DbModel model) { using (var writer = XmlWriter.Create(GetFilePath(contextType), new XmlWriterSettings { Indent = true })) { EdmxWriter.WriteEdmx(model, writer); } }
public void Read_loads_edmx_from_EdmxWriter_into_DbCompiledModel() { var stream = new MemoryStream(); var xmlWriter = XmlWriter.Create(stream); using (var context = new EmptyContext()) { EdmxWriter.WriteEdmx(context, xmlWriter); } stream.Seek(0, SeekOrigin.Begin); var defaultSchema = "default"; var xmlReader = XmlReader.Create(stream); var readModel = EdmxReader.Read(xmlReader, defaultSchema); Assert.IsType <DbCompiledModel>(readModel); Assert.Equal(defaultSchema, readModel.DefaultSchema); Assert.NotNull(readModel.ProviderInfo); }