简体   繁体   中英

EF6 Code First From Database. Edit template at propertyConfiguration

I have installed the nuget package for my Entity Framework 6.x project that exposes the Code Templates. I am editing EntityType.cs.t4. I have identified the code I would like to change.

I would like to change [Required] (below) to [Required(AllowEmptyStrings=true)]

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

In the template this code appears to control that var propertyConfigurations = edm.GetConfigurations(property, Model).OfType();

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

How do I make the change?

Here is how I got it. Hope this helps someone else:

    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) #>
<#
        }
    }
#>

Here are my steps to debug: a) copy in the code lines identified below b) save the template c) remove or comment out app.config connection string (so a new one can be created) d) Delete the existing EntModel.cs file e)Right click on the project and choose add new (data --> Entity framework --> Code First from Database) model. d) The app will prompt you to open a new version of vs 2015 debugger and you can then debug.


This line exists at the very top

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

Code Lines To Copy 1) Replace the line above with this line

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

2) paste this in where you want to break

<#  
System.Diagnostics.Debugger.Launch();  
System.Diagnostics.Debugger.Break(); 
#>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM