将 ExecuteQuery() 与实体框架、实体类一起使用

发布于 2024-10-12 10:17:53 字数 350 浏览 7 评论 0原文

我正在尝试从 ASP Classic 跳转到 asp.net。我已按照教程让实体框架和 LINQ 连接到我的测试数据库,但我在弄清楚 ExecuteQuery() 方面遇到了困难。我相信问题是我的数据库需要一个“实体类”,但我不知道该怎么做。这是我的简单代码:

Dim db as New TestModel.TestEntity
Dim results AS IEnumerable(OF ???) = db.ExecuteQuery(Of ???)("Select * from Table1")

在微软示例站点中,他们使用一个名为“Customers”的实体类,但我不明白这意味着什么。

I am trying to jump from ASP Classic to asp.net. I have followed tutorials to get Entity Framework and LINQ to connect to my test database, but I am having difficulties figuring out ExecuteQuery(). I believe the problem is that I need an "entity class" for my database, but I can't figure out how to do it. Here is my simple code:

Dim db as New TestModel.TestEntity
Dim results AS IEnumerable(OF ???) = db.ExecuteQuery(Of ???)("Select * from Table1")

From the microsoft example site, they use an entity class called Customers, but I don't understand what that means.

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

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

发布评论

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

评论(1

回忆追雨的时光 2024-10-19 10:17:53

实体框架附带了一个可视化设计器。在该设计器中,您连接到现有数据库,然后从数据库中选择要使用的所有表(可能还有视图)。

根据该选择,EF 将为您的每个表生成一个实体类,例如,如果您有一个 Customers 表,您将获得一个 Customer 类,如果您有一个 Products 表,您将获得一个 Product 类。

这些类(默认情况下)以 1:1 的比例表示您的表结构 - 例如,表中的每一列都会转换为该类的属性。

一旦您拥有了它,您就不再需要处理 SQL 语句和诸如 ExecuteQuery() 之类的东西 - 您可以将其留给 EF 来处理。

您只需询问您需要的内容,例如来自给定州的所有客户:

var ohioCustomers = from c in dbContext.Customers
                    where c.State = "OH"
                    select c;

此语句将返回 IEnumerable - 与您的搜索条件匹配的客户列表。

Entity Framework comes with a visual designer. In that designer, you connect to your existing database, and you select all the tables (and possibly views) from your database that you want to work with.

From that selection, EF will generate entity classes one for each of your tables, e.g. if you have a Customers table, you'll get a Customer class, if you have a Products table, you get a Product class.

Those classes represent (by default) your table structure 1:1 - e.g. each column in your table gets translated into a property on the class.

Once you have that, you're no longer dealing with SQL statements and stuff like ExecuteQuery() - you leave that to EF to handle for you.

You just ask for what you need, e.g. all your customers from a given state:

var ohioCustomers = from c in dbContext.Customers
                    where c.State = "OH"
                    select c;

This statement will return an IEnumerable<Customer> - a list of customers that matches your search criteria.

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