【问题标题】:Editing T4 template does not change generated code on Models编辑 T4 模板不会更改模型上生成的代码
【发布时间】:2019-05-08 01:45:03
【问题描述】:

我需要使用数据库优先方法覆盖或替换我的模型的默认构造函数。经过一番研究,我发现这个答案最适合完成我想要的:Override or replace default constructor when using database first approach

所以,我不是专家,我访问了我的 ProjectModel.edmx,在里面我找到了两个扩展名为 .tt 的文件,它们被称为 ProjectModel.Context.ttProjectModel.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     }

我添加了第 25 和 28 行,只是为了通过调用 Init() 生成模型,然后声明方法。

现在,我去了显示我的数据库图表的 ProjectModel.edmx,我在图表上单击鼠标右键并运行 Update Model from Database... 在菜单。然后我转到 Refresh 选项卡并突出显示 Tables 并单击 Finish。我期待新生成的文件与此类似:

namespace Project.Models.DB
{
    using System;
    using System.Collections.Generic;

    public partial class Class1
    {
        public Class1()
        {
            this.Events = new HashSet<Event>();
            Init();
        }

        partial void Init();

        public int id { get; set; }
        public string name { get; set; }
        public string slug { get; set; }
        public bool is_active { get; set; }
        public System.DateTime date_created { get; set; }
        public System.DateTime date_updated { get; set; }

        public virtual ICollection<Event> Events { get; set; }
    }
}

但它不起作用,我想知道我是否需要做其他事情,或者我是否正在编辑正确的文件。任何指导将不胜感激。

【问题讨论】:

    标签: c# visual-studio templates t4 auto-generate


    【解决方案1】:

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

     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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-03
      • 2010-09-22
      • 2013-04-26
      • 2017-06-06
      • 2016-09-06
      • 1970-01-01
      • 2012-09-06
      • 1970-01-01
      相关资源
      最近更新 更多