经常的场景是:交易当前表叫 Table_A
随着数据的积累,一种处理机制是 按日期扩展分成多个 表,表名按照日期扩展如 Table_A_201105,Table_A_201106.....
默认的ActiveRecord配置映射,表名是固定的。
找了一圈,还是官方给了一个方法,不过官方举例的场景是“查询多个表返回”。
CreateSQLQuery
http://www.castleproject.org/activerecord/documentation/trunk/usersguide/nativesql.html
根据我的日期多表的场景,我修改了一下,通过以下代码:
[PrimaryKey]
public int SMT_CardID{get;set;}
[Property]
public DateTime SMT_DealDateTime{get;set;}
public IList<SmartDeal> GetTable(string tb_ext){
string query = string.Format("select SMT_CardID,SMT_DealDateTime from Smart_Deal{0}",tb_ext);
return (IList<SmartDeal>) ActiveRecordMediator<SmartDeal>.Execute(
delegate(ISession session, object instance)
{
return session.CreateSQLQuery(query, "synonym", typeof(SmartDeal))
.List<SmartDeal>();
}, this);
}
public IList<SmartDeal> GetTable2(string tb_ext){
string query = string.Format("select * from Smart_Deal{0}",tb_ext);
return (IList<SmartDeal>) ActiveRecordMediator<SmartDeal>.Execute(
delegate(ISession session, object instance)
{
//return session.CreateSQLQuery(query, "synonym", typeof(SmartDeal)).List<SmartDeal>();
return session.CreateSQLQuery(query).AddEntity("synonym",typeof(SmartDeal)).List<SmartDeal>();
}, this);
}
}
}
这里的实体是SmartDeal,通过GetTable,GetTable2的方法返回加了扩展名的表,GetTable的方法是 官方代码移植的,但是报过时的方法,GetTable2修改