Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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# 如何编写T4模板来创建DTO';来自Entityframework 6?_C#_Entity Framework_Postgresql_Wcf_T4 - Fatal编程技术网

C# 如何编写T4模板来创建DTO';来自Entityframework 6?

C# 如何编写T4模板来创建DTO';来自Entityframework 6?,c#,entity-framework,postgresql,wcf,t4,C#,Entity Framework,Postgresql,Wcf,T4,我有一个大型数据库,在Entityframework中使用数据库优先模型。它位于internet服务器上,通过WCF进行通信。域模型使用所有小写字母作为实体、存储过程和列/属性的名称 在我的客户机应用程序中,我希望使用标准的PascalCase作为命名约定 T4模板能否使用正确的命名约定从Entityframework创建数据传输对象 如果是的话,有人能给我一个如何写的起点吗 为了清楚起见,我不想更改Entityframework生成的任何代码,而是使用Entityframework模型作为另一

我有一个大型数据库,在Entityframework中使用数据库优先模型。它位于internet服务器上,通过WCF进行通信。域模型使用所有小写字母作为实体、存储过程和列/属性的名称

在我的客户机应用程序中,我希望使用标准的PascalCase作为命名约定

T4模板能否使用正确的命名约定从Entityframework创建数据传输对象

如果是的话,有人能给我一个如何写的起点吗

为了清楚起见,我不想更改Entityframework生成的任何代码,而是使用Entityframework模型作为另一个文件的输入,添加具有适当CamelCase命名的简单POCO类,然后该文件可以被WCF服务引用,也可以被Automapper(或类似的东西)引用

谢谢你的建议

  • 数据库:PostgreSQL 9.5
  • 数据库接口:Npgsql 3.0.5
  • .NET 4.5
  • Entityframework 6.0

以下是我创建的T4转换DTOclasses.tt,它只生成简单的类定义,而不是任何人回答这个问题,也希望能帮助像我这样的新手

注意:这不是替换.edmx的.tt文件,而是在.edmx模板生成.edmx文件后运行。(我不想更改用于生成域模型的任何代码)

Visual Studio 2015 Entityframework 6.0 .NET Framework 4.6.1

♦ Notes on Creating DTOclassess.tt

        This T4 transform was created by first copying the working transform used to build the entity model, MedicalOfficeModel.tt.
        Then, parts of it that were not needed for creation of POCO classes to be used for DTO's (data transfer objects) were removed.

    ♦ Changes made to DTOclassses.tt

        •   Adding "DTO" to namespace.
                public void BeginNamespace(CodeGenerationTools code)
                {
                    var codeNamespace = String.Format("{0}.{1}",code.VsNamespaceSuggestion(), "DTO");
                    if (!String.IsNullOrEmpty(codeNamespace))
                    {
                #>
                namespace <#=code.EscapeNamespace(codeNamespace)#>
                {
                <#+
                        PushIndent("    ");
                    }
                }

        •  Put all POCO classes in single file DTOclasses.cs

                <#
                EndNamespace(code);
            }

            fileManager.Process(false);             <--**False stops the splitting of classes into different files. Default is true.

            #>

        •  Change the property naming code:

                    public string Property(EdmProperty edmProperty)
                    {
                        return string.Format(
                            CultureInfo.InvariantCulture,
                            "{0} {1} {2} {{ {3}get; {4}set; }}",
                            Accessibility.ForProperty(edmProperty),
                            _typeMapper.GetTypeName(edmProperty.TypeUsage),
                            GetPascalCase(_code.Escape(edmProperty)),
                            _code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
                            _code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
                    }

        •  Change the class naming code:

                    public string EntityClassOpening(EntityType entity)
                    {
                        return string.Format(
                            CultureInfo.InvariantCulture,
                            "{0} {1}partial class {2}{3}",
                            Accessibility.ForType(entity),
                            _code.SpaceAfter(_code.AbstractOption(entity)),
                            GetPascalCase(_code.Escape(entity)),
                            _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));
                    }

        •  Removed all the navigational stuff. Replaced everything above the helper functions (i.e., above <#+) with:
                            <#@ template debug="false" hostspecific="true" language="C#" #>
                            <#@ assembly name="System.Core" #>
                            <#@ import namespace="System.Linq" #>
                            <#@ import namespace="System.Text" #>
                            <#@ import namespace="System.Collections.Generic" #>
                            <#@ import namespace="System.Text.RegularExpressions" #>
                            <#@ include file="EF6.Utility.CS.ttinclude" #>
                            <#@ output extension=".cs" #>
                            <#

                            const string inputFile = @"MedicalOfficeModel.edmx";
                            var textTransform = DynamicTextTransformation.Create(this);
                            var code = new CodeGenerationTools(this);
                            var ef = new MetadataTools(this);
                            var typeMapper = new TypeMapper(code, ef, textTransform.Errors);
                            var fileManager = EntityFrameworkTemplateFileManager.Create(this);
                            var itemCollection = new EdmMetadataLoader(textTransform.Host, textTransform.Errors).CreateEdmItemCollection(inputFile);
                            var codeStringGenerator = new CodeStringGenerator(code, typeMapper, ef);

                            if (!typeMapper.VerifyCaseInsensitiveTypeUniqueness(typeMapper.GetAllGlobalItems(itemCollection), inputFile))
                            {
                                return string.Empty;
                            }

                            WriteHeader(codeStringGenerator, fileManager);

                            foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
                            {
                                fileManager.StartNewFile(entity.Name + ".cs");
                                BeginNamespace(code);
                            #>
                            <#=codeStringGenerator.UsingDirectives(inHeader: false)#>
                            <#=codeStringGenerator.EntityClassOpening(entity)#>
                            {
                            <#
                                var simpleProperties = typeMapper.GetSimpleProperties(entity);
                                if (simpleProperties.Any())
                                {
                                    foreach (var edmProperty in simpleProperties)
                                    {
                            #>
                                <#=codeStringGenerator.Property(edmProperty)#>
                            <#
                                    }
                                }
                            #>
                            }
                            <#
                                EndNamespace(code);
                            }
                            fileManager.Process(false);

                            #>

        ♦  Added my helper function:
                    <#+

                        public static string GetPascalCase(string name)
                        {
                            return Regex.Replace(name, @"^\w|_\w",
                                (match) => match.Value.Replace("_", "").ToUpper());
                        }
                    #>
♦ 关于创建DTOclassess.tt的注释
该T4转换是通过首先复制用于构建实体模型MedicalOfficeModel.tt的工作转换创建的。
然后,删除了创建用于DTO(数据传输对象)的POCO类所不需要的部分。
♦ 对DTOclassses.tt所做的更改
•在名称空间中添加“DTO”。
public void BeginNamespace(代码生成工具代码)
{
var codeNamespace=String.Format(“{0}.{1}”,code.vsnamespacessuggestion(),“DTO”);
如果(!String.IsNullOrEmpty(codeNamespace))
{
#>
名称空间
{

您好,这会为每个DTO创建单独的分部类吗?我的实体从Sql Server重新创建时,这里会有差异或任何更改。