动态对象查询使用解析的 Entity SQL 针对 EF 4
我正在尝试创建一种完全动态的方式来使用实体sql查询实体框架,其中ObjectQuery中的类型T(表名称或实体名称)在编译时未知,并作为字符串传递到方法中
public class EntityQuery
{
public ObjectContext Context { get; private set; }
public string TableName { get; private set; }
public EntityQuery(ObjectContext context, string sourceObject)
{
MemberInfo[] mInfo = context.GetType().GetMembers();
TableName = sourceObject;
Context = context;
if (!mInfo.Any<MemberInfo>(MemberInfo => MemberInfo.Name == TableName))
throw new EntityOperationException("Entity '" + TableName + "' does not exist in the object context.");
}
public ObjectQuery<dynamic> InitiateQuery(List<EntityFilter> filters)
{
string predicate = DynamicESQLBuilder.GetESQL(filters, Context.DefaultContainerName, TableName);
string format = String.Format("[{0}]", predicate);
ObjectQuery<dynamic> query = new ObjectQuery<dynamic>(predicate, Context, MergeOption.NoTracking);
return query;
}
}
, DynamicESQLBuilder 类根据传入的上下文名称、表名称和实体过滤器创建实体 sql 命令字符串。“EntityFilter”类本质上封装了单个“where”子句,
public class EntityFilter
{
public ExpressionType OpType { get; private set; }
public string PropertyName { get; private set; }
public object Value { get; private set; }
}
当我尝试像这样使用它时,
MyDBModel db = new MyDBModel();
List<EntityFilter> filters = new List<EntityFilter>()
{
new EntityFilter("Name", ExpressionType.Equal, "Sean")
};
EntityQuery query = new EntityQuery(db, "Person");
var results = query.InitiateQuery(filters);
出现错误说明:
“名称”是不是当前加载的架构中“MyDBModel.Person”类型的成员。接近简单标识符,第 1 行,第 74 列。
我愿意采用完全不同的方式来执行此操作。但基本上我想要实现的目标与 WCF OData 服务在幕后所做的类似 - 我可以仅使用传入的字符串来查询我的模型 - 实体的名称、操作、值等。不需要在我的查询中强类型“T”。
I am trying to create a completely dynamic way to query entity framework using entity sql, where the type T in ObjectQuery (the table name, or entity name) is not known at compile time, and is passed into a method as a string,
public class EntityQuery
{
public ObjectContext Context { get; private set; }
public string TableName { get; private set; }
public EntityQuery(ObjectContext context, string sourceObject)
{
MemberInfo[] mInfo = context.GetType().GetMembers();
TableName = sourceObject;
Context = context;
if (!mInfo.Any<MemberInfo>(MemberInfo => MemberInfo.Name == TableName))
throw new EntityOperationException("Entity '" + TableName + "' does not exist in the object context.");
}
public ObjectQuery<dynamic> InitiateQuery(List<EntityFilter> filters)
{
string predicate = DynamicESQLBuilder.GetESQL(filters, Context.DefaultContainerName, TableName);
string format = String.Format("[{0}]", predicate);
ObjectQuery<dynamic> query = new ObjectQuery<dynamic>(predicate, Context, MergeOption.NoTracking);
return query;
}
}
The class DynamicESQLBuilder creates an entity sql command string based on the context name, table name, and entity filters passed in. The 'EntityFilter' class essentially encapsulates a single 'where' clause,
public class EntityFilter
{
public ExpressionType OpType { get; private set; }
public string PropertyName { get; private set; }
public object Value { get; private set; }
}
When I try to use it like this,
MyDBModel db = new MyDBModel();
List<EntityFilter> filters = new List<EntityFilter>()
{
new EntityFilter("Name", ExpressionType.Equal, "Sean")
};
EntityQuery query = new EntityQuery(db, "Person");
var results = query.InitiateQuery(filters);
I get an error stating:
'Name' is not a member of type 'MyDBModel.Person' in the currently loaded schemas. Near simple identifier, line 1, column 74.
I am open to an entirely different way of doing this. But basically what I am trying to achieve is similar to what WCF OData services do behind the scenes -- I can query my model with just strings passed in - the name of the entity, the operation, the value, etc. No need for a strongly typed 'T' in my query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将 Entity SQL 作为字符串,并且不介意以老式 ADO.NET 方式执行操作,则始终可以使用 EntityConnection, EntityCommand 和 EntityDataReader 。
If you have Entity SQL as a string and you don't mind doing things the old-school ADO.NET way, you can always use EntityConnection, EntityCommand, and EntityDataReader directly.