Subsonic 3.0 和大型 SQL 数据库
有没有办法将生成的代码限制为数据库中的特定表?我的数据库有几百个表,我只想在其中少数几个上使用 SubSonic。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
沃伦,我也遇到了和你一样的情况。
你可以尝试我的解决方案。
我的解决方案是一表一T4模板。
您应该做的是:
List LoadTables(params string[]tables)
{
var 结果=新列表();
vartables = LoadTables();
并替换为以下代码: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","");
}
vartables = LoadTables(tableNames);
创建一个名为“Entities”的新文件夹,并将 Settings.ttinclude 和 SQLServer.ttinclude 复制到新文件夹中。
在 ActiveRecord.tt 中,找到代码
vartables = LoadTables();
并替换为以下代码:var tableName = System.IO.Path.GetFileName(Host.TemplateFile).Replace(".tt","");
vartables = LoadTables(tableName);
完成步骤1到步骤4后,当你想从指定的表生成一个新的ActiveRecord类时,你应该做的是:
顺便说一句:我的英语不是很好,所以...
Warren, I also met the same situation as you.
You can try my solution.
My solution is one table one T4 template.
What you should do is :
List LoadTables(params string[] tables)
{
var result=new List();
}
In Context.tt, find code
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);
Create a new folder named "Entities" and copy the Settings.ttinclude and SQLServer.ttinclude to the new folder.
In ActiveRecord.tt, find code
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:
BTW: My engilsh is not very well, so...
如果表名与您感兴趣的表之一匹配,您可以修改 T4 模板以限制函数调用。现在,它仅检查 ExcludedTables。看起来只添加一个白名单也是非常简单的。试试这个。
在 Settings.ttinclude 中,复制
声明,然后粘贴名为“IncludeTables”的新数组声明。将您的表名称添加到其中。然后在 Structs.tt、ActiveRecord.tt 和 Context.tt 中搜索“ExcludeTables”。无论您在哪里找到它,请添加对包含的表格的检查。所以改变,
这
应该可以让你开始。
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. It looks like it would be pretty trivial to just add a whitelist as well. Try this.
In Settings.ttinclude, copy the
declaration, and paste a new array declaration called "IncludeTables". Add your table names to it. Then in Structs.tt, ActiveRecord.tt and Context.tt, do a search for "ExcludeTables". Wherever you find it, add in a check for your included tables. So change,
to
That should get you started.