sfreelander
sfreelander

Reputation: 161

Using ExecuteQuery() with entity framework, entity class

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.

Upvotes: 1

Views: 1866

Answers (1)

marc_s
marc_s

Reputation: 754488

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.

Upvotes: 1

Related Questions