动态对象查询使用解析的 Entity SQL 针对 EF 4

发布于 2024-11-13 01:17:14 字数 2049 浏览 2 评论 0原文

我正在尝试创建一种完全动态的方式来使用实体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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城°AllureLove 2024-11-20 01:17:14

如果您将 Entity SQL 作为字符串,并且不介意以老式 ADO.NET 方式执行操作,则始终可以使用 EntityConnection, EntityCommandEntityDataReader

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文