开发者

Using ExecuteQuery() with entity framework, entity class

开发者 https://www.devze.com 2023-02-04 19:45 出处:网络
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

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 开发者_StackOverflow中文版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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消