0

Is it possible to batch rename the Class (Entity) names in auto generated .cs files inside edmx model? I need to have uppercase first letter for all the entities e.g. "account" needs to be changed to "Account".

partial class account
    {
        public long id { get; set; }
        public int idPartner { get; set; }
        public string accountName { get; set; }
    }

Visual Studio has been generating lower case names for MySql tables used in the project which has been deployed in production & the table names cannot be changed. I know that mappings can be changed using property window in edmx designer but it is not useful in this case because of so many tables. Is there a tool which can do this? If there is none, then what are the exact steps to modify the edmx & other files so that I can create a tool for batch modification of all the entities?

Mustafa R
  • 73
  • 1
  • 8

1 Answers1

0

I'm not sure whether Entity Framework 6 has improved the situation over EF 5. I had a similar issue a while back with EF 5 and found a solution wherein I was able to successfully transform the VS-generated EDMX. Basically, it's a console app that takes in an EDMX file and transforms it.

I merely (successfully) used the tool. You'll find the original with notes here: https://www.tabsoverspaces.com/233202-changing-entity-names-i-e-removing-prefix-in-entity-frameworks-edmx-in-batch/

Credit to Jiří Činčura (www.tabsoverspaces.com)

Below is the code:

static void TransformEDMXEntities(string inputFile, string outputFile)
{
    XDocument xdoc = XDocument.Load(inputFile);

    const string CSDLNamespace = "http://schemas.microsoft.com/ado/2009/11/edm";
    const string MSLNamespace = "http://schemas.microsoft.com/ado/2009/11/mapping/cs";
    const string DesignerNamespace = "http://schemas.microsoft.com/ado/2009/11/edmx";
    XElement csdl = xdoc.Descendants(XName.Get("Schema", CSDLNamespace)).First();
    XElement msl = xdoc.Descendants(XName.Get("Mapping", MSLNamespace)).First();
    XElement designerDiagram = xdoc.Descendants(XName.Get("Designer", DesignerNamespace)).First();

    Func<string, string> transformation = (string input) =>
    {
        const string PREFIX = "tbl";
        Regex re = new Regex(string.Format(@"{0}(\w+)", Regex.Escape(PREFIX)), RegexOptions.None);
        return re.Replace(input, new MatchEvaluator(
            (Match m) =>
            {
                return m.Groups[1].Value;
            }));
    };

    TransformCSDL(CSDLNamespace, csdl, transformation);
    TransformMSL(MSLNamespace, msl, transformation);
    TransformDesigner(DesignerNamespace, designerDiagram, transformation);

    using (XmlTextWriter writer = new XmlTextWriter(outputFile, Encoding.Default))
    {
        xdoc.WriteTo(writer);
    }
}

static void TransformDesigner(string DesignerNamespace, XElement designerDiagram, Func<string, string> transformation)
{
    foreach (var item in designerDiagram.Elements(XName.Get("EntityTypeShape", DesignerNamespace)))
    {
        item.Attribute("EntityType").Value = transformation(item.Attribute("EntityType").Value);
    }
}

static void TransformMSL(string MSLNamespace, XElement msl, Func<string, string> transformation)
{
    foreach (var entitySetMapping in msl.Element(XName.Get("EntityContainerMapping", MSLNamespace)).Elements(XName.Get("EntitySetMapping", MSLNamespace)))
    {
        entitySetMapping.Attribute("Name").Value = transformation(entitySetMapping.Attribute("Name").Value);
        foreach (var entityTypeMapping in entitySetMapping.Elements(XName.Get("EntityTypeMapping", MSLNamespace)))
        {
            entityTypeMapping.Attribute("TypeName").Value = transformation(entityTypeMapping.Attribute("TypeName").Value);
        }
    }
}

static void TransformCSDL(string CSDLNamespace, XElement csdl, Func<string, string> transformation)
{
    foreach (var entitySet in csdl.Element(XName.Get("EntityContainer", CSDLNamespace)).Elements(XName.Get("EntitySet", CSDLNamespace)))
    {
        entitySet.Attribute("Name").Value = transformation(entitySet.Attribute("Name").Value);
        entitySet.Attribute("EntityType").Value = transformation(entitySet.Attribute("EntityType").Value);
    }
    foreach (var associationSet in csdl.Element(XName.Get("EntityContainer", CSDLNamespace)).Elements(XName.Get("AssociationSet", CSDLNamespace)))
    {
        foreach (var end in associationSet.Elements(XName.Get("End", CSDLNamespace)))
        {
            end.Attribute("EntitySet").Value = transformation(end.Attribute("EntitySet").Value);
        }
    }

    foreach (var entityType in csdl.Elements(XName.Get("EntityType", CSDLNamespace)))
    {
        entityType.Attribute("Name").Value = transformation(entityType.Attribute("Name").Value);
    }
    foreach (var association in csdl.Elements(XName.Get("Association", CSDLNamespace)))
    {
        foreach (var end in association.Elements(XName.Get("End", CSDLNamespace)))
        {
            end.Attribute("Type").Value = transformation(end.Attribute("Type").Value);
        }
    }
}
STLDev
  • 5,950
  • 25
  • 36
  • it looks promising. I will check and mark it as answer if works. Thanks anyway. – Mustafa R Jun 07 '17 at 20:51
  • Tried with 2 tables and getting following errors, can you help? 1. The EntityType Model.Account specified as part of this MSL does not exist in MetadataWorkspace 2. At least one property must be mapped in the set mapping for "Accounts" – Mustafa R Jun 08 '17 at 10:34
  • Sincere apologies, but I don't have time at the moment to prove this out. I do not recall running into this issue when I utilized this with EF 5 months ago. – STLDev Jun 09 '17 at 01:40