O/R 将单个任意 SQL SELECT 语句映射到单个类?

发布于 2024-11-04 14:40:25 字数 735 浏览 0 评论 0原文

我不太了解 ORM 工具的工作原理,但通过使用实体框架(EF),我知道它可以以某种方式自动生成来自存储在数据库中的任意视图的 CLR 类。在 EF 中,这些类是从实体模型自动生成的。 EF 甚至可以从存储过程中获取列信息,并根据该信息生成新的“复杂类型”。

  • 如何仅使用 (1) SQL SELECT 语句提供的信息和 (2) 查询数据库元数据的能力来自动生成 .NET 类?

因为 SQL 存储在数据库中,所以我们在编译时就知道 SELECT SQL 是什么。同样,实体框架在编译时知道视图或存储过程的 SELECT SQL 是什么。我们希望通过了解 SELECT SQL 并查询数据库的元数据来自动生成 .NET 类。我需要能够将生成的单个 DataTable 转换为自动生成的类。

看来我的问题的答案在于了解 ORM 工具;

  • 是否有一个 ORM 工具已经可以满足我的需要,或者
  • 您能解释一下实体框架如何从视图和存储过程自动生成其 .NET 类吗?
  • 或者,如果您能为我的场景提出另一种自动生成类的解决方案,我也将不胜感激。

如果需要任何澄清,请告诉我。

I don't know much about the mechanics of how ORM tools work, but from working with Entity Framework (EF), I know it's somehow possible to auto-generate CLR classes from arbitrary Views stored in a database. In EF these classes are auto-generated from your entity model. EF can even get column information from a stored procedure and generate a new "complex type" from this information.

  • How is it possible auto-generate .NET classes, using only (1) the information provided by the SQL SELECT statement and (2) the ability to query the database for its metadata?

Because the SQL is stored in a database, we know at compile time what the SELECT SQL is. Similarly, Entity Framework knows what a View or stored procedure's SELECT SQL is at compile time. We want to auto-generate .NET classes from knowing the SELECT SQL and querying the database's metadata. I need to be able to transform the resulting single DataTable into the auto-generated classes.

It seems like the answer to my question lies in having knowledge of ORM tools;

  • is there an ORM tool out there that can already do what I need, or
  • could you explain how Entity Framework auto-generates its .NET classes from Views and Stored Procedures?
  • Or if you can come up with another solution to auto-generating classes for my scenario, that would also be appreciated.

If any clarification is needed, just let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

余罪 2024-11-11 14:40:25

有关表和视图的元数据从系统视图加载:sys.tablessys.viewssys.columns其他

编辑:

我刚刚测试了 EF 如何获取列名称和类型,它确实执行存储过程并迭代返回的列。它仅使用一些复杂的查询来正确构建调用命令并处理返回值。

Metadata about tables and views are loaded from system views: sys.tables, sys.views, sys.columns and others.

Edit:

I just tested how EF gets column names and types and it indeed executes stored procedure and iterates over returned columns. It only uses some complex queries to correctly build call command and handle return value.

夏至、离别 2024-11-11 14:40:25

如果您使用 System.Data.SqlClient 命名空间,实际上并不需要 ORM 来执行此操作。这是一个适用于随机查询语句的简单示例:

public static List<string> GetColumns(string connectionString, string queryString)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(queryString, connection);

        connection.Open();
        List<string> columnNames = new List<string>();
        SqlDataReader reader = adapter.SelectCommand.ExecuteReader();
        for (int xx = 0; xx < reader.FieldCount; xx++)
        {
            columnNames.Add(reader.GetName(xx));
        }
        return columnNames;
    }
}

这并不能帮助您自动生成类本身。尽管如此,它仍然是提取所需数据的起点。

You actually don't really need an ORM to do this if you use the System.Data.SqlClient namespace. Here's a quick-and-dirty example that works for random query statements:

public static List<string> GetColumns(string connectionString, string queryString)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(queryString, connection);

        connection.Open();
        List<string> columnNames = new List<string>();
        SqlDataReader reader = adapter.SelectCommand.ExecuteReader();
        for (int xx = 0; xx < reader.FieldCount; xx++)
        {
            columnNames.Add(reader.GetName(xx));
        }
        return columnNames;
    }
}

This doesn't help you auto-generate the classes per se. Still, it's a starting point for pulling the data you'll need for doing so.

酒与心事 2024-11-11 14:40:25

我想出了如何做到这一点。我对此写了一个非常基本的解决方案 here

I figured out how to do this. I wrote a very basic solution to this here.

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