1

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.

0

1 Answer 1

1

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.

3
  • I am able to perform LINQ queries, but I can't get executequery() to work. I believe it has to do with the entity classes. I have chosen the tables within the EF, and I thought the classes were the names designated in the EF, but they are not being recognized ie IEnumerable(Of testtable) Jan 16, 2011 at 18:45
  • Why do you even want an ExecuteQuery() ?? What for?? With EF, you basically don't go around executing SQL commands directly....
    – marc_s
    Jan 16, 2011 at 20:47
  • ...yes but for the times where a simple GROUP BY in pure SQL takes less time than EF it would be nice to execute a pure SQL much in the same way LINQ to SQL allows...
    – Smithy
    Feb 4, 2013 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.