SubSonic 生成代码并始终过滤记录
我有一个名为“用户”的表,其中有一列名为“已删除”,这是一个布尔值,指示用户已从系统中“删除”(当然,没有实际删除它)。
我还有很多表对 Users.user_id 列具有 FK。 Subsonic 以类似的方式(非常好地)生成所有外键的代码:
public IQueryable<person> user
{
get
{
var repo=user.GetRepo();
return from items in repo.GetAll()
where items.user_id == _user_id
select items;
}
}
虽然这很好,但是有没有一种方法可以以这样的方式生成代码来始终过滤掉“已删除”用户?
在办公室里,我们能想到的唯一建议是使用部分类并扩展它。当有很多很多类使用 User 表时,这显然是一种痛苦,更不用说很容易无意中使用错误的属性(本例中是 User 与 ActiveUser):
public IQueryable<User> ActiveUser
{
get
{
var repo=User.GetRepo();
return from items in repo.GetAll()
where items.user_id == _user_id and items.deleted == 0
select items;
}
}
有什么想法吗?
I have a table called "Users" that has a column called "deleted", a boolean indicating that the user is "Deleted" from the system (without actually deleting it, of course).
I also have a lot of tables that have a FK to the Users.user_id column. Subsonic generates (very nicely) the code for all the foreign keys in a similar manner:
public IQueryable<person> user
{
get
{
var repo=user.GetRepo();
return from items in repo.GetAll()
where items.user_id == _user_id
select items;
}
}
Whilst this is good and all, is there a way to generate the code in such a way to always filter out the "Deleted" users too?
In the office here, the only suggestion we can think of is to use a partial class and extend it. This is obviously a pain when there are lots and lots of classes using the User table, not to mention the fact that it's easy to inadvertently use the wrong property (User vs ActiveUser in this example):
public IQueryable<User> ActiveUser
{
get
{
var repo=User.GetRepo();
return from items in repo.GetAll()
where items.user_id == _user_id and items.deleted == 0
select items;
}
}
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要更改 ActiveRecord.tt 文件中的以下代码并重新生成代码:
以下代码位于:
#region 'Foreign Keys'
更新: 我已更新您的评论代码,用于检查删除列是否可用,然后仅应用删除条件。
HasLogicalDelete()
- 如果表有“deleted”或“isdeleted”列,此函数将返回 true,否则返回 false。You need to change following code in your ActiveRecord.tt file and regenerate your code:
Following is code is located under :
#region ' Foreign Keys '
Update: I've updated code for your comment for checking if delete column is available then only apply delete condition.
HasLogicalDelete()
- This function will return true if table has "deleted" or "isdeleted" column, false otherwise.你用的是Subsonic3吗?如果是这样,那么您实际上可以编辑模板来修改数据访问层类的生成方式。
Are you using Subsonic3? If so then you can actually edit the templates to modify the way the Data Access Layer classes are generated.