EF6 Code First来自数据库。在propertyConfiguration

时间:2015-09-05 00:44:37

标签: c# entity-framework t4

我已经为我的Entity Framework 6.x项目安装了nuget包,它暴露了代码模板。我正在编辑EntityType.cs.t4。我已经确定了我想要改变的代码。

我想将[必需](下方)更改为[必需(AllowEmptyStrings = true)]

    [Required]
    [StringLength(100)]
    public string Address1 { get; set; }

在模板中,此代码似乎可以控制它             var propertyConfigurations = edm.GetConfigurations(property,Model).OfType();

    foreach (var propertyConfiguration in propertyConfigurations)
    {
#>
    <#= code.Attribute(propertyConfiguration) #>
<#
    }

如何进行更改?

1 个答案:

答案 0 :(得分:1)

这是我如何得到它。希望这有助于其他人:

    var propertyConfigurations = edm.GetConfigurations(property, Model).OfType<IAttributeConfiguration>();

    foreach (var propertyConfiguration in propertyConfigurations)
    {
        if (code.Attribute(propertyConfiguration) == "[Required]")
        {
#>
    <#= "[Required(AllowEmptyStrings=true)]" #>
<#
        }
        else
        {
#>
    <#= code.Attribute(propertyConfiguration) #>
<#
        }
    }
#>

以下是我调试的步骤: a)在下面标识的代码行中复制 b)保存模板 c)删除或注释掉app.config连接字符串(这样就可以创建一个新的连接字符串) d)删除现有的EntModel.cs文件 e)右键单击该项目,然后选择add new(data - &gt; Entity framework - &gt; Code First from Database)模型。 d)应用程序将提示您打开vs 2015调试器的新版本,然后可以调试。

此行位于最顶层

<#@ template visibility="internal" linePragmas="false" #>

要复制的代码行 1)用这一行替换上面的行

<#@ template language="C#" debug="true" hostspecific="true"#>

2)将其粘贴到您想要破解的地方

<#  
System.Diagnostics.Debugger.Launch();  
System.Diagnostics.Debugger.Break(); 
#>
相关问题