将 ExecuteQuery() 与实体框架、实体类一起使用
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实体框架附带了一个可视化设计器。在该设计器中,您连接到现有数据库,然后从数据库中选择要使用的所有表(可能还有视图)。
根据该选择,EF 将为您的每个表生成一个实体类,例如,如果您有一个
Customers
表,您将获得一个Customer
类,如果您有一个Products
表,您将获得一个Product
类。这些类(默认情况下)以 1:1 的比例表示您的表结构 - 例如,表中的每一列都会转换为该类的属性。
一旦您拥有了它,您就不再需要处理 SQL 语句和诸如 ExecuteQuery() 之类的东西 - 您可以将其留给 EF 来处理。
您只需询问您需要的内容,例如来自给定州的所有客户:
此语句将返回
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 aCustomer
class, if you have aProducts
table, you get aProduct
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:
This statement will return an
IEnumerable<Customer>
- a list of customers that matches your search criteria.