EF power tool使用数据注释而不是 Fluent API

来源:互联网 发布:js 打开ie浏览器 编辑:程序博客网 时间:2024/05/21 20:19

某些情况下,您可能希望更改生成代码的方式。例如,生成的代码使用 Fluent API 配置模型(如下所示),但您可能希望改用数据注释。

本节的其余部分演示如何修改代码生成,使表\列映射配置为使用数据注释而不是 Fluent API(如下所示)。

  • 右键单击项目,然后选择“实体框架”–>“自定义反向工程模板”

    选择“自定义反向工程模板”,然后将 T4 模板添加到项目中。随后 EF Power Tools 使用这些模板为上下文、实体和映射类生成代码。



    注意:您可能会看到因 Visual Studio 尝试验证该模板所导致的以下错误。由于我们不会在项目中运行这些模板,可以忽略该错误。


    正在编译转换: 找不到类型或命名空间名称“EfTextTemplateHost”(是否缺少 using 指令或程序集引用?)

现在需要编辑这些模板。

  • 打开 Mapping.tt 文件。
    首先,我们将删除生成 Fluent API 表\列映射的代码
  • 在文件中搜索 var tableSet = efHost.TableSetstring(确保不要粘贴空格字符)。
  • 删除从此行开始到 // Find m:m relationships to configure 上方的行结束的代码。
    下面是要删除的代码:

        var tableSet = efHost.TableSet;

        var tableName = (string)tableSet.MetadataProperties["Table"].Value
            ?? tableSet.Name;
        var schemaName = (string)tableSet.MetadataProperties["Schema"].Value;
    #>
                // Table & Column Mappings
    <#
        if (schemaName == "dbo" || string.IsNullOrWhiteSpace(schemaName))
        {
    #>
                this.ToTable("<#= tableName #>");
    <#
        }
        else
        {
    #>
                this.ToTable("<#= tableName #>", "<#= schemaName #>");
    <#
        }
        foreach (var property in efHost.EntityType.Properties)
        {
    #>
                this.Property(t => t.<#= property.Name #>).HasColumnName("<#= efHost.PropertyToColumnMappings[property].Name #>");
    <#
        }

 

  • 保存 Mapping.tt 文件
  • 打开 Entity.tt 文件
    现在,我们将添加数据注释映射以包含 [Table] 和 [Column] 属性。 我们根据刚删除的 Fluent API 代码添加数据注释代码。当然,您可以进一步进行修改。 
  • 将添加 [Table] 属性的代码(显示为黑色)粘贴到显示为浅灰色的行的后面。

    namespace <#= code.EscapeNamespace(efHost.Namespace) #>

    {
    <#
        var tableSet = efHost.TableSet;
        var tableName = (string)tableSet.MetadataProperties["Table"].Value
            ?? tableSet.Name;
        var schemaName = (string)tableSet.MetadataProperties["Schema"].Value;
    #>
    <#
        if (schemaName == "dbo" || string.IsNullOrWhiteSpace(schemaName))
        {
    #>
        [Table("<#= tableName #>")]
    <#
        }
        else
        {
    #>
        [Table("<#= tableName #>", Schema="<#= schemaName #>")]
    <#
    }
    #>

 

  • 将添加 [Column] 属性的代码(显示为黑色)粘贴到显示为浅灰色的行的后面。

    foreach (var property in efHost.EntityType.Properties)
        {

    #>
            [Column("<#= efHost.PropertyToColumnMappings[property].Name #>")]
    <#

 

  • 我们还需要添加一个 using 语句来指定定义数据注释的位置。在最新版实体框架中,数据注释在 System.ComponentModel.DataAnnotations.Schema 中定义。在以前的版本中,它们在 System.ComponentModel.DataAnnotations 中定义。我们将添加以下逻辑,以便根据 EF 版本添加正确的 using 语句。

    using System;

    using System.Collections.Generic;
    <#
           if (efHost.EntityFrameworkVersion >= new Version(4, 4))
            {
    #>
    using System.ComponentModel.DataAnnotations.Schema;
    <#
            }
            else
            {
    #>
    using System.ComponentModel.DataAnnotations;
    <#
            }
    #>

 

  • 保存 Entity.tt 文件
  • 重复反向工程过程,这一次将使用自定义模板生成代码

 

注意:对 T4 模板进行更改后,必须执行以下操作才能使这些更改反映在代码生成中:

  • 更新 .tt 文件后将其保存
  • 执行“对 Code First 进行反向工程”
0 0
原创粉丝点击