Reverse engineering a subset of tables for Entity Framework

后端 未结 4 1735
耶瑟儿~
耶瑟儿~ 2020-12-31 06:19

I\'m using EF, and I\'ve got the Entity Framework Power Tools extension, which lets me reverse engineer classes based on tables in a given database.

We have a rathe

4条回答
  •  旧时难觅i
    2020-12-31 06:54

    An old question with an answer but it's the only one that comes up from google search so I figured I'd post my solution for Entity Framework Power Tools filter a subset of tables.

    • After installing VSIX EF Power tools go ahead and right click a solution and select Entity Framework --> Custom Reverse Engineer Templates. A few .tt files will be generated under Sln-root/CodeTemplates/ReverseEngineerCodeFirst, open each one of them.

    • Context.tt - find the foreach loop at line 27 and paste code for filtering inside of the foreach loop:

      // START Filter for specific tables only
      string[] filterTables = new string[] { "Motorcycle", "Person" };
      if (!filterTables.Contains(set.ElementType.Name.ToString()))
      {
          continue;   
      }
      // END Filter for specific tables only
      

    This will escape generating public DbSet< lines for tables not specified in your filterTables array.

    In line 38 inside foreach loop paste the same code as above. This will escape generating modelBuilder.Configurations.Add.

    • Entity.tt - find line 6 and paste code which will return empty and escape generating file for tables not in filterTables array:

      // START Filter for specific tables only
      string[] filterTables = new string[] { "Motorcycle", "Person" };
      if (!filterTables.Contains(efHost.EntityType.Name.ToString()))
      {
          return "";  
      }
      // END Filter for specific tables only
      
    • Mapping.tt - find line 14 and paste the same code as above:

      // START Filter for specific tables only
      string[] filterTables = new string[] { "Motorcycle", "Person" };
      if (!filterTables.Contains(efHost.EntityType.Name.ToString()))
      {
          return "";  
      }
      // END Filter for specific tables only
      
    • Save all .tt files and regenerate the model with "Reverse Engineer Code First".

提交回复
热议问题