/// <summary>
 /// Called immediately before the .edmx document is saved.
 /// </summary>
 /// <param name="context"></param>
 void IModelTransformExtension.OnBeforeModelSaved(ModelTransformExtensionContext context)
 {
     // context.OriginalDocument = The .edmx document as created by the Entity Designer.
     //
     // context.CurrentDocument = Make changes to this document using OriginalDocument as a reference.
     //                           This should be the valid .edmx document to be saved.
 }
        public void GetProperties(ModelTransformExtensionContext context)
        {
            var edmxDoc = context.CurrentDocument;

            //Check for existance and enabled. The property is stored in the Conceptual section.
            var property = edmxDoc.Descendants(XName.Get("EdmxAutomationEnabled", "http://schemas.tempuri.com/EdmxAutomationEnabledDesignerExtension")).FirstOrDefault();
            if (property == null)
            {
                //If not present, we default to false;
                EdmxAutomationEnabled = false;
            }
            else
            {
                //If present and false we accept incoming EDMX..
                EdmxAutomationEnabled = Convert.ToBoolean(property.Value);
            }

            //Check for existance and enabled. The property is stored in the Conceptual section.
            property = edmxDoc.Descendants(XName.Get("RefreshOnSaveEnabled", "http://schemas.tempuri.com/RefreshOnSaveEnabledDesignerExtension")).FirstOrDefault();
            if (property == null)
            {
                //If not present, we default to false;
                RefreshOnSaveEnabled = false;
            }
            else
            {
                //If present and false we accept incoming EDMX..
                RefreshOnSaveEnabled = Convert.ToBoolean(property.Value);
            }
        }
 /// <summary>
 /// Called after the .edmx document has been loaded, but before the contents are displayed in the
 /// Entity Designer.
 /// </summary>
 /// <param name="context"></param>
 void IModelTransformExtension.OnAfterModelLoaded(ModelTransformExtensionContext context)
 {
     // context.OriginalDocument = The incoming .edmx document.
     //                            This file cannot be modified.
     //
     // context.CurrentDocument = Make changes to this document using OriginalDocument as a reference.
     //                           This document will be loaded by the Entity Designer.
 }
        public void ReGenerateSsdlMslAndDdl(ModelTransformExtensionContext context)
        {
            var path = context.ProjectItem.FileNames[0];

            //The version of the Entity Framework loaded in PC.
            //The EF Designer offers an upgrade if the version of the EDMX is lower.
            var EfVersion = context.EntityFrameworkVersion;

            //This is the incoming EDMX document
            var edmxDoc = context.CurrentDocument;
            var version = _namespaceManager.GetVersionFromEDMXDocument(edmxDoc);

            var edmxns = _namespaceManager.GetEDMXNamespaceForVersion(version);
            var csns = _namespaceManager.GetCSDLNamespaceForVersion(version);
            var ssns = _namespaceManager.GetSSDLNamespaceForVersion(version);
            var msns = _namespaceManager.GetMSLNamespaceForVersion(version);

            XElement CsdlSchema = edmxDoc.Descendants(XName.Get("Schema", csns.NamespaceName)).FirstOrDefault();
            XElement Ssdl = edmxDoc.Descendants(XName.Get("StorageModels", edmxns.NamespaceName)).FirstOrDefault();
            XElement SsdlSchema = edmxDoc.Descendants(XName.Get("Schema", ssns.NamespaceName)).FirstOrDefault();
            XElement MslSchema = edmxDoc.Descendants(XName.Get("Mapping", msns.NamespaceName)).FirstOrDefault();

            XmlReader[] cReaders = { CsdlSchema.CreateReader() };
            IList<EdmSchemaError> cErrors = null;
            EdmItemCollection edmItemCollection = MetadataItemCollectionFactory.CreateEdmItemCollection(cReaders, out cErrors);

            String ssdlOut = String.Empty;
            String mslOut = String.Empty;
            List<Exception> errors = new List<Exception>();

            //Input parameter must be an EdmItemCollection based of the Schema part.
            if (RunCsdlToSsdlAndMslActivity.Generate(edmItemCollection, out ssdlOut, out mslOut, errors))
                return;

            var XssdlOut = XElement.Parse(ssdlOut);
            var XmslOut = XElement.Parse(mslOut);

            SsdlSchema.ReplaceWith(XssdlOut);
            MslSchema.ReplaceWith(XmslOut);

            String dslOut = String.Empty;

            //Input parameter must be a String based of the Schema part of the CsdlToSsdlAndMslActivity output,
            //whereas for Drop statements to be generated, the ExistingSsdl must be provided also based on the
            //Schema section.
            //if (RunSsdlToDslActivity.Generate(XssdlOut.ToString(), SsdlSchema.ToString(), path, out dslOut, errors))
            //{
            //    return;
            //}

            //var sql = dslOut;
        }
        /// <summary>
        /// Called after the .edmx document has been loaded, but before the contents are displayed in the
        /// Entity Designer.
        /// </summary>
        /// <param name="context"></param>
        void IModelTransformExtension.OnAfterModelLoaded(ModelTransformExtensionContext context)
        {
            // context.OriginalDocument = The incoming .edmx document.
            //                            This file cannot be modified.
            //
            // context.CurrentDocument = Make changes to this document using OriginalDocument as a reference.
            //                           This document will be loaded by the Entity Designer.

            XElement root = context.CurrentDocument.Root;
            string edmxNamespace = root.Name.NamespaceName;
            XElement runtime = root.Element(XName.Get("Runtime", edmxNamespace));
            XElement conceptualModels = runtime.Element(XName.Get("ConceptualModels", edmxNamespace));
            XElement schema = conceptualModels.Elements().FirstOrDefault();
            XNamespace schemaNamespace = schema.GetDefaultNamespace();
        }
Пример #6
0
 internal static void DispatchToSerializationExtensions(
     ICollection<Lazy<IModelTransformExtension>> exports, ModelTransformExtensionContext context, bool loading)
 {
     if (exports != null)
     {
         foreach (var exportInfo in exports)
         {
             var extension = exportInfo.Value;
             if (loading)
             {
                 extension.OnAfterModelLoaded(context);
             }
             else
             {
                 extension.OnBeforeModelSaved(context);
             }
         }
     }
 }
        /// <summary>
        /// Stores the four sections from single edmx model into separate files.
        /// </summary>
        /// <param name="edmxDoc"></param>
        /// <param name="path"></param>
        public void StoreEdmxModel(ModelTransformExtensionContext context)
        {
            try
            {
                var path = context.ProjectItem.FileNames[0];

                //The Edmx document always exists and [hopefully] contains the designer surface property that
                //we use to determine whether EDMXFileTools is enabled.
                //The User may have set to true in this session to be saved.
                var edmxDoc = context.CurrentDocument;

                //Update properties as they may have been changed in this session.
                GetProperties(context);

                if (EdmxAutomationEnabled)
                {
                    //Re-create the Ssdl and Msl to ensure everything is Mapped.
                    //Entity Designer does not support custom mappings
                    ReGenerateSsdlMslAndDdl(context);
                }
            }
            catch (Exception ex)
            {
                var e = new ExtensionError(ex.Message, ex.HResult, ExtensionErrorSeverity.Message);
                context.Errors.Add(e);
            }
        }
 /// <summary>
 /// Called immediately before the .edmx document is saved.
 /// 
 /// Save storage, conceptual, and mapping models in separate files and reset the content of the EDMX model.
 /// </summary>
 /// <param name="context"></param>
 void IModelTransformExtension.OnBeforeModelSaved(ModelTransformExtensionContext context)
 {
     //Now carryout the Save option, with option recreation of the Ssdl and Msl.
     new EdmxTools().StoreEdmxModel(context);
 }
 /// <summary>
 /// Called after the .edmx document has been loaded, but before the contents are displayed in the
 /// Entity Designer.
 /// 
 /// Construct an EDMX model by loading storage, conceptual, and mapping model from separate files and moerging them 
 /// into the EDMX model.
 /// </summary>
 /// <param name="context"></param>
 void IModelTransformExtension.OnAfterModelLoaded(ModelTransformExtensionContext context)
 {
     //AfterModelLoaded only used to access Designer Surface Properties
     new EdmxTools().GetProperties(context);
 }