Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在属性上使用Pascal Case表示法生成所有实体框架类?_C#_Entity Framework - Fatal编程技术网

C# 如何在属性上使用Pascal Case表示法生成所有实体框架类?

C# 如何在属性上使用Pascal Case表示法生成所有实体框架类?,c#,entity-framework,C#,Entity Framework,我有一个Oracle数据库,其中列的名称如下:CREATED_BY,EMPLOYEE_FIRST_NAME 生成实体框架类时,我希望列的关联属性类似于:CreatedBy、EmployeeFirstName等 有没有一种不用手动编辑每个属性的方法呢?它实际上可能根本不是关于C代码的。如果您使用的是Oracle,那么我假设它是ModelFirst或DatabaseFirst方法。在这种情况下,代码是基于CSDL生成的。如果您转换属性名,则内容将中断,因为EF将无法找到与CSDL中的属性名对应的类型

我有一个Oracle数据库,其中列的名称如下:CREATED_BY,EMPLOYEE_FIRST_NAME

生成实体框架类时,我希望列的关联属性类似于:CreatedBy、EmployeeFirstName等


有没有一种不用手动编辑每个属性的方法呢?

它实际上可能根本不是关于C代码的。如果您使用的是Oracle,那么我假设它是ModelFirst或DatabaseFirst方法。在这种情况下,代码是基于CSDL生成的。如果您转换属性名,则内容将中断,因为EF将无法找到与CSDL中的属性名对应的类型和属性。此外,一旦重新生成代码(例如保存模型),您将丢失所有更改。您需要做的是相应地更改CSDL(EDMX的概念部分)和MSL(EDMX的映射部分)中属性的名称。因此,您实际上是在研究如何处理Xml。我认为,如果您熟悉这些技术,使用Xslt或LINQtoXML可以相对容易地完成这项工作。如果操作正确,代码生成器/T4模板应使用您提供的名称生成代码。或者,您可以在EF designer中手动更改名称,但这将需要大量工作

大约两周前,我的任务是摆脱Linq Connect作为我们的数据提供商,将EF用于我们的ORM操作。大家都知道,当微软和甲骨文参与进来的时候,事情从来都不容易,因为他们在一起玩得不好。精明的开发人员需要找到Pascal外壳和多重化的解决方案,以便从Oracle数据库生成的实体符合我们的标准。我们不希望带下划线的表名出现在我们的模型上。经过一点思考和发展,终于找到了一个好的解决方案。最终的结果是操作EDMX文件,然后运行T4模板以实现神奇的效果。我们的最终结果将所有实体及其属性转换为Pascal外壳。它还将所有存储函数转换为Pascal大小写。集合的导航属性也都是多元化的。 下面是要遵循的步骤和代码片段。希望这对编码社区中的一些人有所帮助,您可以随时通过twitter上的Seafare_007联系我,提供有用的评论或建议。 这是: 1.使用EF数据模型项生成EDMX。我在VisualStudio2012中使用了EF5.0。 2.编写一个控制台应用程序来处理EDMX和设计器文件。我在应用程序配置中添加了对两者的引用。 3.这应该是它,你会有帕斯卡案件和多元化的实体。您可以根据自己的需要调整Pascal case方法。 4.我在VisualStudio2012和EF5.0上测试了代码。 5.警告:只适用于没有圆点的单字名称空间,基本上模型不能有OrgName.DeptName.namespace,它将只处理OrgName,但是,您可以调整Pascal大小写方法来解决这个问题

控制台应用程序代码如下:

静态void Main(字符串[]参数) {

        string pathFile = string.Empty;
        string designFile = string.Empty;


        //EDMX File location
        if (ConfigurationManager.AppSettings["EDMX"] != null)
        {
            pathFile = ConfigurationManager.AppSettings["EDMX"].ToString();
        }

        //Designer location for EF 5.0
        if (ConfigurationManager.AppSettings["EDMXDiagram"] != null)
        {
            designFile = ConfigurationManager.AppSettings["EDMXDiagram"].ToString();
        }





        XDocument xdoc = XDocument.Load(pathFile);


        const string CSDLNamespace = "http://schemas.microsoft.com/ado/2009/11/edm";
        const string MSLNamespace = "http://schemas.microsoft.com/ado/2009/11/mapping/cs";

        XElement csdl = xdoc.Descendants(XName.Get("Schema", CSDLNamespace)).First();
        XElement msl = xdoc.Descendants(XName.Get("Mapping", MSLNamespace)).First();



        #region CSDL
        foreach (var entitySet in csdl.Element(XName.Get("EntityContainer", CSDLNamespace)).Elements(XName.Get("EntitySet", CSDLNamespace)))
        {
            entitySet.Attribute("Name").Value = PascalCase(entitySet.Attribute("Name").Value);
            entitySet.Attribute("EntityType").Value = PascalCase(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 = PascalCase(end.Attribute("EntitySet").Value);
            }
        }

        foreach (var funtionSet in csdl.Element(XName.Get("EntityContainer", CSDLNamespace)).Elements(XName.Get("FunctionImport", CSDLNamespace)))
        {

            funtionSet.Attribute("Name").Value = PascalCase(funtionSet.Attribute("Name").Value);

        }

        foreach (var entityType in csdl.Elements(XName.Get("EntityType", CSDLNamespace)))
        {
            entityType.Attribute("Name").Value = PascalCase(entityType.Attribute("Name").Value);

            foreach (var key in entityType.Elements(XName.Get("Key", CSDLNamespace)))
            {
                foreach (var propertyRef in key.Elements(XName.Get("PropertyRef", CSDLNamespace)))
                {
                    propertyRef.Attribute("Name").Value = PascalCase(propertyRef.Attribute("Name").Value);
                }
            }

            foreach (var property in entityType.Elements(XName.Get("Property", CSDLNamespace)))
            {
                property.Attribute("Name").Value = PascalCase(property.Attribute("Name").Value);
            }

            foreach (var navigationProperty in entityType.Elements(XName.Get("NavigationProperty", CSDLNamespace)))
            {
                navigationProperty.Attribute("Name").Value = PascalCase(navigationProperty.Attribute("Name").Value, true, true); 
            }

        }
        foreach (var association in csdl.Elements(XName.Get("Association", CSDLNamespace)))
        {
            foreach (var end in association.Elements(XName.Get("End", CSDLNamespace)))
            {
                end.Attribute("Type").Value = PascalCase(end.Attribute("Type").Value);
            }               

            foreach(var refs in association.Elements(XName.Get("ReferentialConstraint", CSDLNamespace)))
            {

                foreach (var pri in refs.Elements(XName.Get("Principal", CSDLNamespace)))
                {

                    foreach (var proref in pri.Elements(XName.Get("PropertyRef", CSDLNamespace)))
                    {

                        proref.Attribute("Name").Value = PascalCase(proref.Attribute("Name").Value);
                    }

                }

                foreach (var pri in refs.Elements(XName.Get("Dependent", CSDLNamespace)))
                {

                    foreach (var proref in pri.Elements(XName.Get("PropertyRef", CSDLNamespace)))
                    {

                        proref.Attribute("Name").Value = PascalCase(proref.Attribute("Name").Value);
                    }

                }


            }



        }
        #endregion

        #region MSL

        foreach (var entitySetMapping in msl.Element(XName.Get("EntityContainerMapping", MSLNamespace)).Elements(XName.Get("EntitySetMapping", MSLNamespace)))
        {
            entitySetMapping.Attribute("Name").Value = PascalCase(entitySetMapping.Attribute("Name").Value);

            foreach (var entityTypeMapping in entitySetMapping.Elements(XName.Get("EntityTypeMapping", MSLNamespace)))
            {
                entityTypeMapping.Attribute("TypeName").Value = PascalCase(entityTypeMapping.Attribute("TypeName").Value);
                foreach
                (var scalarProperty in
                (entityTypeMapping.Element(XName.Get("MappingFragment", MSLNamespace))).Elements(XName.Get("ScalarProperty", MSLNamespace))
                )
                {
                    scalarProperty.Attribute("Name").Value = PascalCase(scalarProperty.Attribute("Name").Value);
                }

            }
        }
        foreach (var associationSetMapping in msl.Element(XName.Get("EntityContainerMapping", MSLNamespace)).Elements(XName.Get("AssociationSetMapping", MSLNamespace)))
        {
            foreach (var endProperty in associationSetMapping.Elements(XName.Get("EndProperty", MSLNamespace)))
            {
                foreach (var scalarProperty in endProperty.Elements(XName.Get("ScalarProperty", MSLNamespace)))
                {
                    scalarProperty.Attribute("Name").Value = PascalCase(scalarProperty.Attribute("Name").Value);
                }
            }
        }

        foreach (var functionSetMapping in msl.Element(XName.Get("EntityContainerMapping", MSLNamespace)).Elements(XName.Get("FunctionImportMapping", MSLNamespace)))
        {
            functionSetMapping.Attribute("FunctionImportName").Value = PascalCase(functionSetMapping.Attribute("FunctionImportName").Value);
        }
        #endregion

        xdoc.Save(pathFile);


        XmlDocument designXml = new XmlDocument();

        designXml.Load(designFile);      


        XmlNamespaceManager dsMan = new XmlNamespaceManager(designXml.NameTable);
        dsMan.AddNamespace("edmx", "http://schemas.microsoft.com/ado/2009/11/edmx");
        dsMan.AddNamespace("d", "http://schemas.microsoft.com/ado/2009/11/edmx");


        #region Designer

        XmlNodeList entitySet1 = designXml.DocumentElement.SelectNodes("//d:Diagrams", dsMan);

        foreach (XmlNode xn in entitySet1)
        {

            foreach (XmlElement xp in xn.ChildNodes)
            {

                foreach (XmlElement z in xp.ChildNodes)
                {

                    if (z.Attributes[0].Name == "EntityType")
                    {

                        z.Attributes[0].Value = PascalCase(z.Attributes[0].Value.ToString(), true);

                    }


                }

            }


        }

        designXml.Save(designFile);


        #endregion

    }

    #region Pluralization


    public static string Pluralize(string name)
    {

   return System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService(new CultureInfo("en-US")).Pluralize(name);
    }



    #endregion



    #region Pascal Casing

    public static string PascalCase(StructuralType type, bool sanitizeName = true)
    {
        if (type == null)
        {
            return null;
        }

        return PascalCase(type.Name, sanitizeName);
    }


    public static string PascalCase(EdmMember member, bool sanitizeName = true)
    {
        if (member == null)
        {
            return null;
        }

        return PascalCase(member.Name, sanitizeName);
    }

    public static string PascalCase(string name, bool sanitizeName = true, bool pluralize = false)
    {

       // if pascal case exists
       // exit function

        Regex rgx = new Regex(@"^[A-Z][a-z]+(?:[A-Z][a-z]+)*$");

        string pascalTest = name;

        if (name.Contains("."))
        {
            string[] test  = new string[]{};
            test = name.Split('.');

            if(rgx.IsMatch(test[1].ToString()))
            {
                return name;
            }

        }
        else
        {

            if (rgx.IsMatch(name))
            {
                return name;
            }

        }

        //Check for dot notations in namespace

        bool contains = false;
        string[] temp = new string[] { };
        var namespc = string.Empty;

        if (name.Contains("."))
        {
            contains = true;
            temp = name.Split('.');
            namespc = temp[0];

        }

        if (contains)
        {
            name = temp[1];
        }

        name = name.ToLowerInvariant();

        string result = name;
        bool upperCase = false;

        result = string.Empty;
        for (int i = 0; i < name.Length; i++)
        {
            if (name[i] == ' ' || name[i] == '_')
            {
                upperCase = true;
            }
            else
            {
                if (i == 0 || upperCase)
                {
                    result += name[i].ToString().ToUpperInvariant();
                    upperCase = false;
                }
                else
                {
                    result += name[i];
                }
            }
        }


        if (contains)
        {


            result = namespc.ToString() + "." + result;



        }

        if (pluralize)
        {
            result = Pluralize(result);
        }


        return result;
    }
    #endregion
string路径文件=string.Empty;
string designFile=string.Empty;
//EDMX文件位置
如果(ConfigurationManager.AppSettings[“EDMX”]!=null)
{
pathFile=ConfigurationManager.AppSettings[“EDMX”].ToString();
}
//EF 5.0的设计者位置
如果(ConfigurationManager.AppSettings[“EDMXDiagram”]!=null)
{
designFile=ConfigurationManager.AppSettings[“EDMXDiagram”].ToString();
}
XDocument xdoc=XDocument.Load(路径文件);
常量字符串CSDLNamespace=”http://schemas.microsoft.com/ado/2009/11/edm";
常量字符串MSLNamespace=”http://schemas.microsoft.com/ado/2009/11/mapping/cs";
XElement csdl=xdoc.substands(XName.Get(“Schema”,CSDLNamespace)).First();
XElement msl=xdoc.substands(XName.Get(“Mapping”,MSLNamespace)).First();
#区域CSDL
foreach(csdl.Element(XName.Get(“EntityContainer”,CSDLNamespace)).Elements(XName.Get(“entitySet,CSDLNamespace)))中的变量entitySet)
{
entitySet.Attribute(“Name”).Value=PascalCase(entitySet.Attribute(“Name”).Value);
entitySet.Attribute(“EntityType”).Value=PascalCase(entitySet.Attribute(“EntityType”).Value);
}
foreach(csdl.Element(XName.Get(“EntityContainer”,CSDLNamespace)).Elements(XName.Get(“associationSet,CSDLNamespace)))中的变量associationSet)
{
foreach(associationSet.Elements(XName.Get(“end”,CSDLNamespace))中的变量end)
{
end.Attribute(“EntitySet”).Value=PascalCase(end.Attribute(“EntitySet”).Value);
}
}
foreach(csdl.Element(XName.Get(“EntityContainer”,CSDLNamespace)).Elements(XName.Get(“FunctionImport”,CSDLNamespace)))中的var FunctionSet)
{
funtionSet.Attribute(“Name”).Value=PascalCase(funtionSet.Attribute(“Name”).Value);
}
foreach(csdl.Elements中的var entityType(XName.Get(“entityType”,CSDLNamespace)))
{
entityType.Attribute(“Name”).Value=PascalCase(entityType.Attribute(“Name”).Value);
foreach(entityType.Elements(XName.Get(“key”,CSDLNamespace))中的var键)
{
foreach(key.Elements(XName.Get(“propertyRef”,CSDLNamespace))中的var propertyRef)
{
propertyRef.Attribute(“Name”).Value=PascalCase(propertyRef.Attribute(“Name”).Value);
}
}
foreach(entityType.Elements(XName.Get(“property”,CSDLNamespace))中的var属性)
{
property.Attribute(“Name”).Value=PascalCase(property.Attribute(“Name”).Value);
}
entityType.E中的foreach(var navigationProperty