首先在实体框架数据库中重命名重命名类

时间:2017-06-07 19:39:41

标签: c# mysql visual-studio entity-framework-6 database-first

是否可以批量重命名edmx模型中自动生成的.cs文件中的Class(Entity)名称?我需要为所有实体设置大写的第一个字母,例如“帐户”需要更改为“帐户”。

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

Visual Studio已为项目中使用的MySql表生成小写名称,该表已在生产和部署中部署。表名无法更改。 我知道可以使用edmx设计器中的属性窗口更改映射,但由于这么多表,它在这种情况下没用。有没有可以做到这一点的工具?如果没有,那么修改edmx&的确切步骤是什么?其他文件,以便我可以创建一个工具批量修改所有实体?

1 个答案:

答案 0 :(得分:0)

我不确定实体框架6是否改进了EF 5的情况。我在EF 5中遇到了类似的问题并找到了一个解决方案,其中我能够成功转换VS生成的EDMX。基本上,它是一个控制台应用程序,它接收EDMX文件并对其进行转换。

我只是(成功地)使用了该工具。你会在这里找到带有注释的原文:https://www.tabsoverspaces.com/233202-changing-entity-names-i-e-removing-prefix-in-entity-frameworks-edmx-in-batch/

感谢JiříČinčura(www.tabsoverspaces.com)

以下是代码:

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);
        }
    }
}
相关问题