Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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模板不会更改模型上生成的代码_C#_Visual Studio_Templates_T4_Auto Generate - Fatal编程技术网

C# 编辑T4模板不会更改模型上生成的代码

C# 编辑T4模板不会更改模型上生成的代码,c#,visual-studio,templates,t4,auto-generate,C#,Visual Studio,Templates,T4,Auto Generate,对于使用数据库优先方法的项目,我需要重写或替换模型的默认构造函数。经过一些研究,我发现这个答案最适合实现我的目标: 所以,我不是专家,我去了ProjectModel.edmx,在里面我找到了两个扩展名为.tt的文件,它们分别叫做ProjectModel.Context.tt和ProjectModel.tt,所以我想我需要编辑第二个文件 为了理解它是如何工作的,我发现了一段代码,它似乎生成了类的构造函数: 1 <# 2 var complexProperties = typeMa

对于使用数据库优先方法的项目,我需要重写或替换模型的默认构造函数。经过一些研究,我发现这个答案最适合实现我的目标:

所以,我不是专家,我去了ProjectModel.edmx,在里面我找到了两个扩展名为.tt的文件,它们分别叫做ProjectModel.Context.tt和ProjectModel.tt,所以我想我需要编辑第二个文件

为了理解它是如何工作的,我发现了一段代码,它似乎生成了类的构造函数:

 1 <#
 2     var complexProperties = typeMapper.GetComplexProperties(complex);
 3     var propertiesWithDefaultValues = typeMapper.GetPropertiesWithDefaultValues(complex);
 4 
 5     if (propertiesWithDefaultValues.Any() || complexProperties.Any())
 6     {
 7 #>
 8     public <#=code.Escape(complex)#>()
 9     {
10 <#
11         foreach (var edmProperty in propertiesWithDefaultValues)
12         {
13 #>
14         this.<#=code.Escape(edmProperty)#> = <#=typeMapper.CreateLiteral(edmProperty.DefaultValue)#>;
15 <#
16         }
17 
18         foreach (var complexProperty in complexProperties)
19         {
20 #>
21         this.<#=code.Escape(complexProperty)#> = new <#=typeMapper.GetTypeName(complexProperty.TypeUsage)#>();
22 <#
23         }
24 #>
25      Init();
26     }
27 
28     partial void Init();
29 
30 <#
31     }

但它不起作用,我想知道我是否需要做其他事情,或者我是否正在编辑正确的文件。非常感谢您的指导。

您的更改位于模板的“复杂类型”部分。注:

 5     if (propertiesWithDefaultValues.Any() || complexProperties.Any())
 6     {
 7 #>
 8     public <#=code.Escape(complex)#>()
 9     {
10 <#
查找实体的迭代,然后在那里进行编辑:

foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
{
    fileManager.StartNewFile(entity.Name + ".cs");
    BeginNamespace(code);
#>
<#=codeStringGenerator.UsingDirectives(inHeader: false)#> // This may be slightly different based on version of EF, but you get the idea
<#=codeStringGenerator.EntityClassOpening(entity)#>
{
<#
    var propertiesWithDefaultValues = typeMapper.GetPropertiesWithDefaultValues(entity);
    var collectionNavigationProperties = typeMapper.GetCollectionNavigationProperties(entity);
    var complexProperties = typeMapper.GetComplexProperties(entity);

    if (propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any())
    {
#>
    public <#=code.Escape(entity)#>()
    {
// ... much later
        foreach (var complexProperty in complexProperties)
        {
#>
        this.<#=code.Escape(complexProperty)#> = new <#=typeMapper.GetTypeName(complexProperty.TypeUsage)#>();
<#
        }
#>
    }
    Init();
}
partial void Init();
foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
{
    fileManager.StartNewFile(entity.Name + ".cs");
    BeginNamespace(code);
#>
<#=codeStringGenerator.UsingDirectives(inHeader: false)#> // This may be slightly different based on version of EF, but you get the idea
<#=codeStringGenerator.EntityClassOpening(entity)#>
{
<#
    var propertiesWithDefaultValues = typeMapper.GetPropertiesWithDefaultValues(entity);
    var collectionNavigationProperties = typeMapper.GetCollectionNavigationProperties(entity);
    var complexProperties = typeMapper.GetComplexProperties(entity);

    if (propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any())
    {
#>
    public <#=code.Escape(entity)#>()
    {
// ... much later
        foreach (var complexProperty in complexProperties)
        {
#>
        this.<#=code.Escape(complexProperty)#> = new <#=typeMapper.GetTypeName(complexProperty.TypeUsage)#>();
<#
        }
#>
    }
    Init();
}
partial void Init();