Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not an issue just a way to import EF 6 EDMX tables and and renaming of the tables/fields #1331

Closed
glenlewisdd opened this issue Apr 8, 2022 · 5 comments
Labels
question Further information is requested

Comments

@glenlewisdd
Copy link

glenlewisdd commented Apr 8, 2022

Here is some code I wrote to quickly export the tables I was using in the old EF6 for the EFCorePowerTools:

Just simply provide the path to your edmx file from the original EF6 and this will create two files the rename and the table list you can copy and paste into the config json for the efpt.

using System.Xml.Serialization;

namespace ExportEDMXToJson.Model.Edmx
{
    public class EFEdmx
    {
        [XmlElement]
        public StorageModel? StorageModels { get; set; }

        [XmlElement]
        public ConceptualModel? ConceptualModels { get; set; }

        [XmlElement]
        public Mappings? Mappings { get; set; }        
    }

    public class StorageModel
    {
        [XmlElement]
        public Schema? Schema { get; set; }
    }

    public class ConceptualModel
    {
        [XmlElement]
        public Schema? Schema { get; set; }
    }

    public class Mappings
    {
        [XmlElement]
        public Mapping Mapping { get; set; }
    }

    public class Schema
    {
        [XmlAttribute]
        public string Namespace { get; set; }

        [XmlAttribute]
        public string Provider { get; set; }

        [XmlAttribute]
        public string ProviderManifestToken { get; set; }

        [XmlAttribute]
        public string Alias { get; set; }

        [XmlElement]
        public List<EntityType> EntityType { get; set; }

        [XmlElement]
        public List<Association> Association { get; set; }

        [XmlElement]
        public List<Function> Function { get; set; }

        [XmlElement]
        public EntityContainer EntityContainer { get; set; }
        
    }

    public class EntityType
    {
        public EntityType()
        {
            //Doc = null;
        }
        [XmlAttribute]
        public string? Name { get; set; }

        [XmlElement]
        public Key Key { get; set; }

        [XmlElement]
        public List<Property> Property { get; set; }

        [XmlElement]
        public List<NavigationProperty> NavigationProperty { get; set; }
    }

    public class Association
    {
        [XmlAttribute]
        public string? Name { get; set; }
    }
    
    public class Function
    {
        [XmlAttribute]
        public string? Name { get; set; }
    }

    public class Key
    {
        [XmlElement]
        public List<PropertyRef> PropertyRef { get; set; }
    }

    public class PropertyRef
    {
        [XmlAttribute]
        public string? Name { get; set; }
    }

    public class Property
    {
        [XmlAttribute]
        public string? Name { get; set; }

        [XmlAttribute]
        public string? Type { get; set; }

        [XmlAttribute]
        public string? Nullable { get; set; }

        [XmlAttribute]
        public string? MaxLength { get; set; }

        [XmlAttribute]
        public string? Precision { get; set; }

        [XmlAttribute]
        public string? Scale { get; set; }

        [XmlAttribute]
        public string? StoreGeneratedPattern { get; set; }

        [XmlAttribute]
        public string? FixedLength { get; set; }

        [XmlAttribute]
        public string? Unicode { get; set; }
    }

    public class NavigationProperty
    {
        [XmlAttribute]
        public string? Name { get; set; }

        [XmlAttribute]
        public string? Relationship { get; set; }

        [XmlAttribute]
        public string? FromRole { get; set; }

        [XmlAttribute]
        public string? ToRole { get; set; }

    }

    public class Mapping
    {
        [XmlAttribute]
        public string Space { get; set; }
        [XmlElement]
        public EntityContainerMapping EntityContainerMapping { get; set; }
    }

    public class EntityContainerMapping
    {
        [XmlAttribute]
        public string StorageEntityContainer { get; set; }

        [XmlAttribute]
        public string CdmEntityContainer { get; set; }

        [XmlElement]
        public List<EntitySetMapping> EntitySetMapping { get; set; }

    }

    public class EntitySetMapping
    {
        [XmlAttribute]
        public string Name { get; set; }

        public EntityTypeMapping EntityTypeMapping { get; set; }
    }

    public class EntityTypeMapping
    {

        [XmlAttribute]
        public string TypeName { get; set; }

        public MappingFragment MappingFragment { get; set; }

    }
    
    public class MappingFragment
    {

        [XmlAttribute]
        public string StoreEntitySet { get; set; }

        [XmlElement]
        public List<ScalarProperty> ScalarProperty { get; set; }

    }

    public class ScalarProperty
    {

        [XmlAttribute]
        public string Name { get; set; }
        [XmlAttribute]
        public string ColumnName { get; set; }
    }

    public class EntityContainer
    {

        [XmlAttribute]
        public string Name { get; set; }

        [XmlElement]
        public List<EntitySet> EntitySet { get; set; }

    }
    
    public class EntitySet
    {

        [XmlAttribute]
        public string Name { get; set; }

        [XmlAttribute]
        public string EntityType { get; set; }

        [XmlAttribute]
        public string Schema { get; set; }

        [XmlAttribute]
        public string storeSchema { get; set; }

        [XmlAttribute]
        public string storeType { get; set; }

        public string TableName
        {
            get
            {
                return string.Format("[{0}].[{1}]", Schema ?? storeSchema, Name);
            }
        }
    }
}

namespace ExportEDMXToJson.Model.EFTool
{
    internal class EFTableList
    {
        public EFTool()
        {

        }

        public EFTool(Edmx.EFEdmx edmx)
        {
            if (edmx?.StorageModels?.Schema != null)
            {
                Tables = edmx.StorageModels.Schema.EntityContainer.EntitySet.Select(s => new Table
                {
                    Name = s.TableName,
                    ObjectType = s.storeType == "Table" ? 0 : 1,
                }).ToList();
            }
            
        }

        public int CodeGenerationMode { get; set; }
        public string ContextClassName { get; set; }
        public string ContextNamespace { get; set; }
        public string DefaultDacpacSchema { get; set; }
        public string FilterSchemas { get; set; }
        public string IncludeConnectionString { get; set; }
        public string ModelNamespace { get; set; }
        public string OutputContextPath { get; set; }
        public string OutputPath { get; set; }
        public string ProjectRootNamespace { get; set; }
        public List<Schema> Schemas { get; set; }
        public int SelectedHandlebarsLanguage { get; set; }
        public int SelectedToBeGenerated { get; set; }
        public List<Table> Tables { get; set; }
    }

    public class Table
    {
        public string Name { get; set; }

        public int ObjectType { get; set; }
    }

    public class Schema
    {
        public string Name { get; set; }
    }

    internal class EFRenameList
    {
        public EFRenameTool()
        {

        }

        public EFRenameTool(Edmx.EFEdmx edmx)
        {
            var tables = edmx.Mappings.Mapping.EntityContainerMapping.EntitySetMapping.Select(s => new TableRename
            {
                NewName = s.Name,
                Name = s.EntityTypeMapping.MappingFragment.StoreEntitySet,
                Columns = s.EntityTypeMapping.MappingFragment.ScalarProperty.Select(col => new ColumnRename()
                {
                    Name = col.ColumnName,
                    NewName = col.Name
                }).ToList()
            }).ToList();

            var tableList = tables.Select(s => s.Name).ToList();

            var schemaList = edmx.StorageModels.Schema.EntityContainer.EntitySet
                .GroupBy(g => new { Schema = g.storeSchema ?? g.Schema })
                .Select(s =>  new { Schema = s.Key.Schema, Tables = s.ToList() })
                .ToList();
            Schemas = new List<SchemaRename>();
            foreach (var schema in schemaList)
            {
                Schemas.Add(new SchemaRename()
                {
                    SchemaName = schema.Schema,
                    UseSchemaName = false,
                    Tables = tables.Where(f => tableList.Contains(f.Name)).ToList()
                }); ;;
            }
            
        }

        //public string SchemaName { get; set; }
        //public bool UseSchemaName { get; set; }

        //public List<TableRename> Tables { get; set; }

        public List<SchemaRename> Schemas { get; set; }
    }

    public class SchemaRename
    {
        public string SchemaName { get; set; }
        public bool UseSchemaName { get; set; }
        public List<TableRename> Tables { get; set; }
    }

    public class TableRename
    {
        public List<ColumnRename> Columns { get; set; }

        public string Name { get; set; }
        public string NewName { get; set; }


    }

    public class ColumnRename
    {

        public string Name { get; set; }
        public string NewName { get; set; }
    }
}

public class ImportEF6TabletoEFCorePT
{
	public void Convert(string pathToEDMX)
	{
		string text = File.ReadAllText(pathToEDMX);
		text = text.Replace("edmx:", string.Empty);
		text = text.Replace("xmlns:", string.Empty);
		text = text.Replace("xmlns", "x");
		text = text.Replace(":", string.Empty);
		File.WriteAllText("temp.xml", text);

		var reader = new ChoXmlReader<EFEdmx>("temp.xml");
		EFEdmx rec = reader.Read();
		if (rec.ConceptualModels != null)
		{

			var efTableList = new EFTableList(rec);
			var efRenameList = new EFRenameList(rec);

			var tableListJson = Newtonsoft.Json.JsonConvert.SerializeObject(efTableList, Formatting.Indented);
			var renameList = Newtonsoft.Json.JsonConvert.SerializeObject(efRenameList, Formatting.Indented);


			File.WriteAllText("efpt.tablelist.json", tableListJson);
			File.WriteAllText("efpt.renaming.json", renameList);
		}

		reader.Close();
		File.Delete("temp.xml");
	}

}
@glenlewisdd
Copy link
Author

Sorry about the formatting, it would not block the code correctly.

@ErikEJ
Copy link
Owner

ErikEJ commented Apr 9, 2022

This is great, I have fixed the formatting. I will add this to the wiki page.

@HansBM
Copy link

HansBM commented Dec 13, 2024

The code does not compile, so here is an update:

Run it in .net core. It requires the nugets ChoETL and Newtonsoft.

using ChoETL;
using ExportEDMXToJson.Model.Edmx;
using ExportEDMXToJson.Model.EFTool;
using Newtonsoft.Json;
using System.Xml.Serialization;


new ImportEF6TabletoEFCorePT().Convert(@"EFDataModel.edmx");


namespace ExportEDMXToJson.Model.Edmx
{
    public class EFEdmx
    {
        [XmlElement]
        public StorageModel? StorageModels { get; set; }

        [XmlElement]
        public ConceptualModel? ConceptualModels { get; set; }

        [XmlElement]
        public Mappings? Mappings { get; set; }
    }

    public class StorageModel
    {
        [XmlElement]
        public Schema? Schema { get; set; }
    }

    public class ConceptualModel
    {
        [XmlElement]
        public Schema? Schema { get; set; }
    }

    public class Mappings
    {
        [XmlElement]
        public Mapping Mapping { get; set; }
    }

    public class Schema
    {
        [XmlAttribute]
        public string Namespace { get; set; }

        [XmlAttribute]
        public string Provider { get; set; }

        [XmlAttribute]
        public string ProviderManifestToken { get; set; }

        [XmlAttribute]
        public string Alias { get; set; }

        [XmlElement]
        public List<EntityType> EntityType { get; set; }

        [XmlElement]
        public List<Association> Association { get; set; }

        [XmlElement]
        public List<Function> Function { get; set; }

        [XmlElement]
        public EntityContainer EntityContainer { get; set; }

    }

    public class EntityType
    {
        public EntityType()
        {
            //Doc = null;
        }
        [XmlAttribute]
        public string? Name { get; set; }

        [XmlElement]
        public Key Key { get; set; }

        [XmlElement]
        public List<Property> Property { get; set; }

        [XmlElement]
        public List<NavigationProperty> NavigationProperty { get; set; }
    }

    public class Association
    {
        [XmlAttribute]
        public string? Name { get; set; }
    }

    public class Function
    {
        [XmlAttribute]
        public string? Name { get; set; }
    }

    public class Key
    {
        [XmlElement]
        public List<PropertyRef> PropertyRef { get; set; }
    }

    public class PropertyRef
    {
        [XmlAttribute]
        public string? Name { get; set; }
    }

    public class Property
    {
        [XmlAttribute]
        public string? Name { get; set; }

        [XmlAttribute]
        public string? Type { get; set; }

        [XmlAttribute]
        public string? Nullable { get; set; }

        [XmlAttribute]
        public string? MaxLength { get; set; }

        [XmlAttribute]
        public string? Precision { get; set; }

        [XmlAttribute]
        public string? Scale { get; set; }

        [XmlAttribute]
        public string? StoreGeneratedPattern { get; set; }

        [XmlAttribute]
        public string? FixedLength { get; set; }

        [XmlAttribute]
        public string? Unicode { get; set; }
    }

    public class NavigationProperty
    {
        [XmlAttribute]
        public string? Name { get; set; }

        [XmlAttribute]
        public string? Relationship { get; set; }

        [XmlAttribute]
        public string? FromRole { get; set; }

        [XmlAttribute]
        public string? ToRole { get; set; }

    }

    public class Mapping
    {
        [XmlAttribute]
        public string Space { get; set; }
        [XmlElement]
        public EntityContainerMapping EntityContainerMapping { get; set; }
    }

    public class EntityContainerMapping
    {
        [XmlAttribute]
        public string StorageEntityContainer { get; set; }

        [XmlAttribute]
        public string CdmEntityContainer { get; set; }

        [XmlElement]
        public List<EntitySetMapping> EntitySetMapping { get; set; }

    }

    public class EntitySetMapping
    {
        [XmlAttribute]
        public string Name { get; set; }

        public EntityTypeMapping EntityTypeMapping { get; set; }
    }

    public class EntityTypeMapping
    {

        [XmlAttribute]
        public string TypeName { get; set; }

        public MappingFragment MappingFragment { get; set; }

    }

    public class MappingFragment
    {

        [XmlAttribute]
        public string StoreEntitySet { get; set; }

        [XmlElement]
        public List<ScalarProperty> ScalarProperty { get; set; }

    }

    public class ScalarProperty
    {

        [XmlAttribute]
        public string Name { get; set; }
        [XmlAttribute]
        public string ColumnName { get; set; }
    }

    public class EntityContainer
    {

        [XmlAttribute]
        public string Name { get; set; }

        [XmlElement]
        public List<EntitySet> EntitySet { get; set; }

    }

    public class EntitySet
    {

        [XmlAttribute]
        public string Name { get; set; }

        [XmlAttribute]
        public string EntityType { get; set; }

        [XmlAttribute]
        public string Schema { get; set; }

        [XmlAttribute]
        public string storeSchema { get; set; }

        [XmlAttribute]
        public string storeType { get; set; }

        public string TableName
        {
            get
            {
                return string.Format("[{0}].[{1}]", Schema ?? storeSchema, Name);
            }
        }
    }
}

namespace ExportEDMXToJson.Model.EFTool
{
    internal class EFTableList
    {
        public EFTableList()
        {

        }

        public EFTableList(Edmx.EFEdmx edmx)
        {
            if (edmx?.StorageModels?.Schema != null)
            {
                Tables = edmx.StorageModels.Schema.EntityContainer.EntitySet.Select(s => new Table
                {
                    Name = s.TableName,
                    ObjectType = s.storeType == "Table" ? 0 : 1,
                }).ToList();
            }

        }

        public int CodeGenerationMode { get; set; }
        public string ContextClassName { get; set; }
        public string ContextNamespace { get; set; }
        public string DefaultDacpacSchema { get; set; }
        public string FilterSchemas { get; set; }
        public string IncludeConnectionString { get; set; }
        public string ModelNamespace { get; set; }
        public string OutputContextPath { get; set; }
        public string OutputPath { get; set; }
        public string ProjectRootNamespace { get; set; }
        public List<Schema> Schemas { get; set; }
        public int SelectedHandlebarsLanguage { get; set; }
        public int SelectedToBeGenerated { get; set; }
        public List<Table> Tables { get; set; }
    }

    public class Table
    {
        public string Name { get; set; }

        public int ObjectType { get; set; }
    }

    public class Schema
    {
        public string Name { get; set; }
    }

    internal class EFRenameList
    {
        public EFRenameList()
        {

        }

        public EFRenameList(Edmx.EFEdmx edmx)
        {
            var tables = edmx.Mappings.Mapping.EntityContainerMapping.EntitySetMapping.Select(s => new TableRename
            {
                NewName = s.Name,
                Name = s.EntityTypeMapping.MappingFragment.StoreEntitySet,
                Columns = s.EntityTypeMapping.MappingFragment.ScalarProperty.Select(col => new ColumnRename()
                {
                    Name = col.ColumnName,
                    NewName = col.Name
                }).ToList()
            }).ToList();

            var tableList = tables.Select(s => s.Name).ToList();

            var schemaList = edmx.StorageModels.Schema.EntityContainer.EntitySet
                .GroupBy(g => new { Schema = g.storeSchema ?? g.Schema })
                .Select(s => new { Schema = s.Key.Schema, Tables = s.ToList() })
                .ToList();
            Schemas = new List<SchemaRename>();
            foreach (var schema in schemaList)
            {
                Schemas.Add(new SchemaRename()
                {
                    SchemaName = schema.Schema,
                    UseSchemaName = false,
                    Tables = tables.Where(f => tableList.Contains(f.Name)).ToList()
                }); ; ;
            }

        }

        //public string SchemaName { get; set; }
        //public bool UseSchemaName { get; set; }

        //public List<TableRename> Tables { get; set; }

        public List<SchemaRename> Schemas { get; set; }
    }

    public class SchemaRename
    {
        public string SchemaName { get; set; }
        public bool UseSchemaName { get; set; }
        public List<TableRename> Tables { get; set; }
    }

    public class TableRename
    {
        public List<ColumnRename> Columns { get; set; }

        public string Name { get; set; }
        public string NewName { get; set; }


    }

    public class ColumnRename
    {

        public string Name { get; set; }
        public string NewName { get; set; }
    }
}

public class ImportEF6TabletoEFCorePT
{
    public void Convert(string pathToEDMX)
    {
        string text = File.ReadAllText(pathToEDMX);
        text = text.Replace("edmx:", string.Empty);
        text = text.Replace("xmlns:", string.Empty);
        text = text.Replace("xmlns", "x");
        text = text.Replace(":", string.Empty);
        File.WriteAllText("temp.xml", text);

        var reader = new ChoXmlReader<EFEdmx>("temp.xml");
        EFEdmx rec = reader.Read();
        if (rec.ConceptualModels != null)
        {

            var efTableList = new EFTableList(rec);
            var efRenameList = new EFRenameList(rec);

            var tableListJson = Newtonsoft.Json.JsonConvert.SerializeObject(efTableList, Formatting.Indented);
            var renameList = Newtonsoft.Json.JsonConvert.SerializeObject(efRenameList, Formatting.Indented);


            File.WriteAllText("efpt.tablelist.json", tableListJson);
            File.WriteAllText("efpt.renaming.json", renameList);
        }

        reader.Close();
        File.Delete("temp.xml");
    }

}

@ErikEJ
Copy link
Owner

ErikEJ commented Dec 13, 2024

Thanks a lot @HansBM - I will update / ensure a link from the wiki

@ErikEJ
Copy link
Owner

ErikEJ commented Dec 14, 2024

@HansBM Added to the docs, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants