gpt4 book ai didi

c# - 首先批量重命名 Entity Framework 数据库中的类

转载 作者:行者123 更新时间:2023-11-29 18:49:35 25 4
gpt4 key购买 nike

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

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

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

最佳答案

我不确定 Entity Framework 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);
}
}
}

关于c# - 首先批量重命名 Entity Framework 数据库中的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44421471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com