petapoco 动态获取完整实体

xueweil xueweil 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
现在是用这种方法来得到一个类的完整实体。

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;
}
...全文
评论 1
收藏 已收藏
分享
返回顶部
xueweil
C# 111,129 成员 642,540 内容