internal void CreateCsdlAndMslFile(string csdlFilePath, string mslFilePath) { if (Store == null) { throw new Exception("Can't create the CSDL and MSL files, because the `Store` object is null."); } Models = new EntityModelSchemaGenerator(Store.StoreItemCollection, "ShoppingModelConceptual", "ShoppingModelConceptualContainer"); Models.GenerateMetadata(); Models.WriteModelSchema(csdlFilePath); Models.WriteStorageMapping(mslFilePath); }
public EdmMapping(EntityModelSchemaGenerator mcGenerator, StoreItemCollection store) { var mappingDoc = new XmlDocument(); var mappingXml = new StringBuilder(); using (var textWriter = new StringWriter(mappingXml)) mcGenerator.WriteStorageMapping(new XmlTextWriter(textWriter)); mappingDoc.LoadXml(mappingXml.ToString()); var entitySets = mcGenerator.EntityContainer.BaseEntitySets.OfType <EntitySet>(); var associationSets = mcGenerator.EntityContainer.BaseEntitySets.OfType <AssociationSet>(); var tableSets = store.GetItems <EntityContainer>().Single().BaseEntitySets.OfType <EntitySet>(); EntityMappings = BuildEntityMappings(mappingDoc, entitySets, tableSets); ManyToManyMappings = BuildManyToManyMappings(mappingDoc, associationSets, tableSets); }
public void MyTest() { var names = new[] { "Simple", "Strings", "AllClrTypes", "GuidIdentity", "DateTime", "CompositeKey", "InverseMapping", "OneToMany", "ManyToMany", "AdventureWorks" }; //var reverse = new ReverseEngineerCodeFirstHandler(); foreach (var name in names) { // Load store schema var storeGenerator = new EntityStoreSchemaGenerator( "System.Data.SqlClient", $"Server=localhost;Database={name};Trusted_Connection=True;", "dbo"); storeGenerator.GenerateForeignKeyProperties = true; var errors = storeGenerator.GenerateStoreMetadata(Test.StoreMetadataFilters) .Where(x => x.Severity == System.Data.Metadata.Edm.EdmSchemaErrorSeverity.Error) .ToArray(); storeGenerator.WriteStoreSchema(@"c:\temp\" + name + ".ssdl"); //errors.HandleErrors(Strings.ReverseEngineer_SchemaError); var reader = XmlReader.Create(@"c:\temp\" + name + ".ssdl"); IList <EdmSchemaError> theErrors; var storeItemCollection = StoreItemCollection.Create(new[] { reader }, null, null, out theErrors); //var storeItemCollection = new System.Data.Metadata.Edm.StoreItemCollection(new[] { reader }); //var blahCC = new EntityModelSchemaGenerator(storeItemCollection, "My", "Default"); //blahCC.PluralizationService = PluralizationService.CreateService(new CultureInfo("en")); //blahCC.GenerateForeignKeyProperties = true; //blahCC.GenerateMetadata(); //MetadataWorkspace workspace = new MetadataWorkspace( // () => storeItemCollection, // null, // null); var blahB1 = storeItemCollection.GetEntityContainer("dboContainer"); var blahB2 = storeItemCollection.OfType <EntityType>().ToArray(); // Generate default mapping var contextName = "MyContext"; var modelGenerator = new EntityModelSchemaGenerator(storeGenerator.EntityContainer, "DefaultNamespace", contextName); modelGenerator.PluralizationService = PluralizationService.CreateService(new CultureInfo("en")); modelGenerator.GenerateForeignKeyProperties = true; modelGenerator.GenerateMetadata(); modelGenerator.WriteModelSchema(@"C:\temp\" + name + ".csdl"); modelGenerator.WriteStorageMapping(@"c:\temp\" + name + ".msdl"); } // Pull out info about types to be generated //var entityTypes = modelGenerator.EdmItemCollection.OfType<EntityType>().ToArray(); //var mappings = new ReverseEngineerCodeFirstHandler.EdmMapping(modelGenerator, storeItemCollection); //var contextHost = new EfTextTemplateHost //{ // EntityContainer = modelGenerator.EntityContainer, // Namespace = "MyNamespace", // ModelsNamespace = "MyNamespace.Model", // MappingNamespace = "MyNamespace.Mapping", // EntityFrameworkVersion = new Version(5,0), //}; //var templateProcessor = new TemplateProcessor(); //var contextContents = templateProcessor.ProcessContext(contextHost); //foreach (var entityType in entityTypes) //{ // // Generate the code file // var entityHost = new EfTextTemplateHost // { // EntityType = entityType, // EntityContainer = modelGenerator.EntityContainer, // Namespace = "MyNamespace", // ModelsNamespace = "MyNamespace.Model", // MappingNamespace = "MyNamespace.Mapping", // EntityFrameworkVersion = new Version(5, 0), // TableSet = mappings.EntityMappings[entityType].Item1, // PropertyToColumnMappings = mappings.EntityMappings[entityType].Item2, // ManyToManyMappings = mappings.ManyToManyMappings // }; // var entityContents = templateProcessor.ProcessEntity(entityHost); //} //foreach (var entityType in entityTypes) //{ // var mappingHost = new EfTextTemplateHost // { // EntityType = entityType, // EntityContainer = modelGenerator.EntityContainer, // Namespace = "MyNamespace", // ModelsNamespace = "MyNamespace.Model", // MappingNamespace = "MyNamespace.Mapping", // EntityFrameworkVersion = new Version(5, 0), // TableSet = mappings.EntityMappings[entityType].Item1, // PropertyToColumnMappings = mappings.EntityMappings[entityType].Item2, // ManyToManyMappings = mappings.ManyToManyMappings // }; // var mappingContents = templateProcessor.ProcessMapping(mappingHost); //} //reverse.ReverseEngineerCodeFirst(); Assert.True(true); }
// End Added ErikEJ #region the functions that actually do the interesting things private static void ModelGen( string connectionString, string provider, string modelName, Version version, bool includeForeignKeys, bool pluralize, List <string> tables) { IList <EdmSchemaError> ssdlErrors = null; IList <EdmSchemaError> csdlAndMslErrors = null; // generate the SSDL string ssdlNamespace = modelName + "Model.Store"; EntityStoreSchemaGenerator essg = new EntityStoreSchemaGenerator( provider, connectionString, ssdlNamespace); essg.GenerateForeignKeyProperties = includeForeignKeys; //ErikEJ Filter selected tables var filters = new List <EntityStoreSchemaFilterEntry>(); foreach (var table in tables) { var entry = new EntityStoreSchemaFilterEntry(null, null, table); filters.Add(entry); } ssdlErrors = essg.GenerateStoreMetadata(filters, version); // detect if there are errors or only warnings from ssdl generation bool hasSsdlErrors = false; bool hasSsdlWarnings = false; if (ssdlErrors != null) { foreach (EdmSchemaError e in ssdlErrors) { if (e.Severity == EdmSchemaErrorSeverity.Error) { hasSsdlErrors = true; } else if (e.Severity == EdmSchemaErrorSeverity.Warning) { hasSsdlWarnings = true; } } } // write out errors & warnings if (hasSsdlErrors && hasSsdlWarnings) { //System.Console.WriteLine("Errors occurred during generation:"); WriteErrors(ssdlErrors); } // if there were errors abort. Continue if there were only warnings if (hasSsdlErrors) { return; } // write the SSDL to a string using (StringWriter ssdl = new StringWriter()) { XmlWriter ssdlxw = XmlWriter.Create(ssdl); essg.WriteStoreSchema(ssdlxw); ssdlxw.Flush(); // generate the CSDL string csdlNamespace = modelName + "Model"; string csdlEntityContainerName = modelName + "Entities"; EntityModelSchemaGenerator emsg = new EntityModelSchemaGenerator( essg.EntityContainer, csdlNamespace, csdlEntityContainerName); emsg.GenerateForeignKeyProperties = includeForeignKeys; // Begin Added ErikEJ if (pluralize) { emsg.PluralizationService = System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService(new System.Globalization.CultureInfo("en-US")); } // End Added ErikEJ csdlAndMslErrors = emsg.GenerateMetadata(version); // detect if there are errors or only warnings from csdl/msl generation bool hasCsdlErrors = false; bool hasCsdlWarnings = false; if (csdlAndMslErrors != null) { foreach (EdmSchemaError e in csdlAndMslErrors) { if (e.Severity == EdmSchemaErrorSeverity.Error) { hasCsdlErrors = true; } else if (e.Severity == EdmSchemaErrorSeverity.Warning) { hasCsdlWarnings = true; } } } // write out errors & warnings if (hasCsdlErrors || hasCsdlWarnings) { //System.Console.WriteLine("Errors occurred during generation:"); WriteErrors(csdlAndMslErrors); } // if there were errors, abort. Don't abort if there were only warnigns. if (hasCsdlErrors) { return; } // write CSDL to a string using (StringWriter csdl = new StringWriter()) { XmlWriter csdlxw = XmlWriter.Create(csdl); emsg.WriteModelSchema(csdlxw); csdlxw.Flush(); // write MSL to a string using (StringWriter msl = new StringWriter()) { XmlWriter mslxw = XmlWriter.Create(msl); emsg.WriteStorageMapping(mslxw); mslxw.Flush(); // write csdl, ssdl & msl to the EDMX file ToEdmx( csdl.ToString(), ssdl.ToString(), msl.ToString(), new FileInfo( modelName + ".edmx"), includeForeignKeys, pluralize); } } } }
internal override bool Generate() { IList <EdmSchemaError> errors = null; // generate the SSDL string ssdlNamespace = _modelName + "Model.Store"; EntityStoreSchemaGenerator essg = new EntityStoreSchemaGenerator(ProviderName, _con.ConnectionString, ssdlNamespace); List <EntityStoreSchemaFilterEntry> filters = new List <EntityStoreSchemaFilterEntry>(); if (_tables != null && _tables.Count > 0) { foreach (var tablename in _tables) { filters.Add(new EntityStoreSchemaFilterEntry(null, null, tablename, EntityStoreSchemaFilterObjectTypes.Table, EntityStoreSchemaFilterEffect.Allow)); } } else { filters.Add(new EntityStoreSchemaFilterEntry(null, null, _table, EntityStoreSchemaFilterObjectTypes.Table, EntityStoreSchemaFilterEffect.Allow)); } filters.Add(new EntityStoreSchemaFilterEntry(null, null, "%", EntityStoreSchemaFilterObjectTypes.View, EntityStoreSchemaFilterEffect.Exclude)); Version entityVersion = new Version(2, 0, 0, 0); #if NET_40_OR_GREATER errors = essg.GenerateStoreMetadata(filters, entityVersion); #else errors = essg.GenerateStoreMetadata(filters); #endif // write out errors if ((errors != null && errors.Count > 0)) { WriteErrors(errors); SendErrorsToGeneralOuput(); } // write the SSDL to a string StringWriter ssdl = new StringWriter(); XmlWriter ssdlxw = XmlWriter.Create(ssdl); essg.WriteStoreSchema(ssdlxw); ssdlxw.Flush(); // generate the CSDL string csdlNamespace = _artifactNamespace; string csdlEntityContainerName = _modelName + "Entities"; EntityModelSchemaGenerator emsg = new EntityModelSchemaGenerator(essg.EntityContainer, csdlNamespace, csdlEntityContainerName); #if NET_40_OR_GREATER emsg.GenerateForeignKeyProperties = true; errors = emsg.GenerateMetadata(entityVersion); #else errors = emsg.GenerateMetadata(); #endif // write out errors if ((errors != null && errors.Count > 0)) { WriteErrors(errors); SendErrorsToGeneralOuput(); } // write CSDL to a string StringWriter csdl = new StringWriter(); XmlWriter csdlxw = XmlWriter.Create(csdl); emsg.WriteModelSchema(csdlxw); csdlxw.Flush(); // write MSL to a string StringWriter msl = new StringWriter(); XmlWriter mslxw = XmlWriter.Create(msl, new XmlWriterSettings() { }); emsg.WriteStorageMapping(mslxw); mslxw.Flush(); // Write everything + glue xml to file string file = Path.Combine(_path, _modelName + ".edmx"); string sCsdl = csdl.ToString(); string sSsdl = ssdl.ToString(); string sMsl = msl.ToString(); sCsdl = sCsdl.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", ""); sSsdl = sSsdl.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", ""); sMsl = sMsl.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", ""); FileInfo fi = new FileInfo(file); WriteEdmx(sCsdl, sSsdl, sMsl, fi); if (_mappings != null && _mappings.Count != 0) { GetColumnMappings(fi); } AddToProject(fi.FullName); if (_warnings.Count > 0) { SendErrorsToGeneralOuput(); _tablesIncluded = GetTablesInModel(fi.FullName); } else { _tablesIncluded = _tables; } return(true); }