c# - 如何编写 T4 模板以从 Entity Framework 6 创建 DTO?

标签 c# entity-framework postgresql wcf t4

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

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

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

如果是这样,有人可以给我一个如何编写它的起点吗?

明确一点,我不想更改 Entityframework 生成的任何代码,而是使用 Entityframework 模型添加简单的 POCO 类,并使用适当的 CamelCase 命名作为另一个文件的输入,然后可以由WCF 服务,可能还有 Automapper(或类似的东西)。

感谢您的任何建议。

  • 数据库:PostgreSQL 9.5
  • 数据库接口(interface):Npgsql 3.0.5
  • .NET 4.5
  • Entity Framework 6.0

最佳答案

代替任何人回答这个问题并希望帮助像我这样的其他新手,这是我创建 T4 转换 DTOclasses.tt 的方法,它只产生简单的类定义。

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

Visual Studio 2015 Entity Framework 6.0 .NET 框架 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());
                        }
                    #>

当一切都完成后,它会完美运行(在 VS2015 中)以完全满足我的需要。 :)

关于c# - 如何编写 T4 模板以从 Entity Framework 6 创建 DTO?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35468335/

相关文章:

c# - ProtoBuf 不反序列化具有接口(interface)的对象

c# - 是否有在现有模板中添加 css 模板的解决方案?

entity-framework - EntityFramework : Calling ToList() on IQueryable with ~11. 000 条记录需要 10 秒

mysql - 处理 .NET Core project.json 中的依赖冲突

entity-framework - LINQPad 4 无法找到 EntityFramework

javascript - 在 Angularjs 中通过 IP 地址获取用户的位置

c# - 如何使用 C# 代码触发 XML (XSLT) 转换?如何将参数 ("param"s) 传递给 XSLT?

node.js - 如何使用 Node.js 和 Knex 连接到 AWS EC2 实例内的 postgresql 数据库

ruby - 从 Postgres 时间戳中添加或减去时间

java - 使用 Java exec 的额外 psql 命令行参数