petapoco 动态获取完整实体

xueweil 2015-01-29 10:56:54
现在是用这种方法来得到一个类的完整实体。

public partial class Group:BaseEntity<Group>

private int? _GroupId;
[Column("group_id")]
public int? GroupId
{
get{return _GroupId;}
set{_GroupId=value;}
}
private List<Role> _Roles = new List<Role>();
[ResultColumn]
public List<Role> Roles
{
get { return Role.Fetch("where group_id=@0",_GroupId); }
set { _Roles = value; }
}


这样做就得不停的写where...参考了activerecord写了个HasManyAttribute来标识关系,但是不知道如何动态获取值。下面的field.PropertyType是错误的,不知道是不是我的想法有问题?

public static T FindOneFull(object primaryKey) {
T entity=Database.SingleOrDefault<T>(primaryKey);
PropertyInfo[] fields = typeof(T).GetProperties();
foreach (var field in fields)
{
var att=field.GetCustomAttributes(typeof(HasManyAttribute), true);
if (att.Length != 0)
{

string sql="select * from "+ ((HasManyAttribute)att[0]).TableName +" where " + ((HasManyAttribute)att[0]).ColumnKey + "=@0";
var lst = BaseEntity<field.PropertyType>.Fetch(sql, primaryKey);
field.SetValue(entity, lst,null);
}
}
return entity;
}
...全文
144 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
xueweil 2015-02-02
  • 打赏
  • 举报
回复
還是自己處理吧。。。結貼。

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧