简体   繁体   English

Subsonic 3.0和大型SQL数据库

[英]Subsonic 3.0 and large SQL Database

Is there a way to limit the code generated to specific tables in a database? 有没有一种方法可以将生成的代码限制为数据库中的特定表? My database has a few hundred tables and I only really want to use SubSonic on a handfull of them. 我的数据库有几百张表,我真的只想在其中几张表上使用SubSonic。

You can modify the T4 templates to just limit the function call if the table name matches one of the tables you're interested in. Right now, it just checks for ExcludedTables. 您可以修改T4模板以仅在表名与您感兴趣的表之一匹配时限制函数调用。现在,它仅检查ExcludedTables。 It looks like it would be pretty trivial to just add a whitelist as well. 似乎只添加一个白名单也将是微不足道的。 Try this. 尝试这个。

In Settings.ttinclude, copy the 在Settings.ttinclude中,复制

string[] ExcludeTables = new string[]{
"sysdiagrams",
"BuildVersion",
};

declaration, and paste a new array declaration called "IncludeTables". 声明,然后粘贴一个名为“ IncludeTables”的新数组声明。 Add your table names to it. 将您的表名称添加到它。 Then in Structs.tt, ActiveRecord.tt and Context.tt, do a search for "ExcludeTables". 然后在Structs.tt,ActiveRecord.tt和Context.tt中搜索“ ExcludeTables”。 Wherever you find it, add in a check for your included tables. 无论您在哪里找到它,请添加对包含表的检查。 So change, 所以改变

if(!ExcludeTables.Contains(tbl.Name))

to

if(!ExcludeTables.Contains(tbl.Name) 
    && (IncludeTables.Length == 0 || IncludeTables.Contains(tbl.Name))

That should get you started. 那应该让您开始。

Warren, I also met the same situation as you. 沃伦,我也遇到过与您相同的情况。 You can try my solution. 您可以尝试我的解决方案。 My solution is one table one T4 template. 我的解决方案是一张桌子一张T4模板。

What you should do is : 您应该做的是:

  1. In SQLServer.ttinclude, replace LoadTables method with the code below: 在SQLServer.ttinclude中,将LoadTables方法替换为以下代码:

List LoadTables(params string[] tables) { var result=new List(); 列出LoadTables(参数字符串[]表){var result = new List();

string sql = TABLE_SQL;
if(tables.Length > 0)
{
  StringBuilder sb = new StringBuilder();
  foreach(string table in tables){
    sb.AppendFormat("'{0}',", table);
  }
  sql += " and TABLE_NAME in (" + sb.Remove(sb.Length - 1, 1).ToString() + ")";
}

//pull the tables in a reader
using(IDataReader rdr=GetReader(sql))
{
    while(rdr.Read())
    {
        Table tbl=new Table();
        tbl.Name=rdr["TABLE_NAME"].ToString();
        tbl.Schema=rdr["TABLE_SCHEMA"].ToString();
        tbl.Columns=LoadColumns(tbl);
        tbl.PrimaryKey=GetPK(tbl.Name);
        tbl.CleanName=CleanUp(tbl.Name);
        tbl.ClassName=Inflector.MakeSingular(tbl.CleanName);
        tbl.QueryableName=Inflector.MakePlural(tbl.ClassName);

        //set the PK for the columns
        var pkColumn=tbl.Columns.SingleOrDefault(x=>x.Name.ToLower().Trim()==tbl.PrimaryKey.ToLower().Trim());
        if(pkColumn!=null)
            pkColumn.IsPK=true;

        tbl.FKTables=LoadFKTables(tbl.Name);

        result.Add(tbl);
    }
}

foreach(Table tbl in result)
{
    //loop the FK tables and see if there's a match for our FK columns
    foreach(Column col in tbl.Columns)
    {
        col.IsForeignKey=tbl.FKTables.Any(
            x=>x.ThisColumn.Equals(col.Name,StringComparison.InvariantCultureIgnoreCase)
        );
    }
}
return result;

} }

  1. In Context.tt, find code var tables = LoadTables(); 在Context.tt中,找到代码var tables = LoadTables(); and replace with the code below: 并替换为以下代码:

    var dir = System.IO.Path.GetDirectoryName(Host.TemplateFile) + "\\\\Entities"; var fileNames = Directory.GetFiles(dir, "*.tt"); string[] tableNames = new string[fileNames.Length]; for(int i=0; i < fileNames.Length; i++) { tableNames[i] = System.IO.Path.GetFileName(fileNames[i]).Replace(".tt",""); } var tables = LoadTables(tableNames);

  2. Create a new folder named "Entities" and copy the Settings.ttinclude and SQLServer.ttinclude to the new folder. 创建一个名为“ Entities”的新文件夹,然后将Settings.ttinclude和SQLServer.ttinclude复制到新文件夹。

  3. In ActiveRecord.tt, find code var tables = LoadTables(); 在ActiveRecord.tt中,找到代码var tables = LoadTables(); and replace with the code below: 并替换为以下代码:

    var tableName = System.IO.Path.GetFileName(Host.TemplateFile).Replace(".tt",""); var tables = LoadTables(tableName);

After finish step1 to step4, when you want to generate a new ActiveRecord Class from a specified table, what you should do is: 完成步骤1到步骤4之后,当您想从指定的表生成新的ActiveRecord类时,您应该做的是:

  1. Create a new T4 template that has the same name as the specified table's tablename. 创建一个新的T4模板,该模板的名称与指定表的表名相同。
  2. Copy the ActiveRecord.tt code to the new T4 template 将ActiveRecord.tt代码复制到新的T4模板
  3. Generate code for the new T4 template 为新的T4模板生成代码
  4. Generate code for the Context.tt template 为Context.tt模板生成代码

BTW: My engilsh is not very well, so... 顺便说一句:我的英语不是很好,所以...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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