Entity framework run direct query - c#

I'm thinking about using Entity Framework in an ASP.NET application, using an Oracle database.
I would also need to know is I can run a query directly on the database tables and data, using Entity Framework, without using the classes and the mappings.
Thanks!

ExecuteStoreQuery can be used.
However, part of the beauty/fun/elegance of using Entity is being able to write your queries using LINQ and not having to write actual SQL statements.
Also, just because you decide to use Entity, doesn't mean you can no longer use SqlCommand objects etc...

You could use ExecuteStoreQuery() for that. Be aware though that you a have to provide a type that all returned columns can be mapped to, it does not have to be an entity though.

Related

ADO.NET vs EntityFramework

Can someone please explain the difference between ADO.NET and Entity Framework in layman's terms?
I have searched from Google but can't understand the difference.
ADO.Net means using sqlConnection();, sqlCommand(); etc. to interact with database using queries?
Entity Framework means using db.Add();, db.SaveChanges(); functions to interact with database without using queries? Am I right?
When you use EF db.Add(); or db.SaveChanges or any other integrated EF method, the ORM (object-relational mapper), in this example EF, will use ADO.NET (so EF will open database connection using ADO.NET, EF will create "SQL query" using ADO.NET,...).
Of course, you can do this all by yourself, using ADO.NET methods, which sometimes can increase the performance of the queries, but usually needs more code writing.
But in general, when you use EF, you also use ADO.NET, only its implemented inside EF methods.

use Entity Framework sql generator manually / EF without entity data model

I'm looking a way to use Entity Framework to build SQL queries to DB without using Entity Data Models.
The reason why I'm looking a way to use EF is it has database providers for MySql, SQL, SQLite and I wish I had opportunity to use any of them in my projects.
There is classes that implement DbExpressionVisitor< T > in every provider so I think maybe there is a way to construct some DbCommandTree objects to pass it to SqlProviderServices for example.
I want to achive almost the same functionality as dynasql or Kerosene ORM has but with good community support. I know I will need to create some proxy classes to use DbExpressions in fluent way.

Difference between Native SQL and Entity SQL

I have gone through an article of using EntityConnection, EntityCommands for executing Entity sql queries. But I was unable to understand that Why are we using Entity sql? Why not directly using the Classes and objects for processing CRUD operations on database?
Or If we want to execute sql queries then why we are using Entity Sql , why not directly Ado.net ?
Is there any performance difference or something else?
I have already gone through the page http://msdn.microsoft.com/en-us/library/bb738573.aspx. But I want answer in a more simpler way. Can you please answer me?
Thanks
Why not directly using the Classes and objects for processing CRUD operations on database?
That is (should be) the way for almost all operations. But sometimes there is a need for accessing the db more directly and precisely.
If we want to execute sql queries then why we are using Entity Sql , why not directly Ado.net ?
E-SQL will still let you work with entities (instead of Rows). This is much easier and more powerful, consider inheritance for example.
E-SQL is also supposed to be independent of the actual database, ie the same for Oracle etc. I have no experience with this yet.
Is there any performance difference or something else?
It can be used to improve performance, yes. But not automatically.
The main difference
SQL is database dependent query language working on storage (relational) objects - tables / rows
ESQL is database independent query language working on conceptual (EDMX) objects - entities
ESQL was created prior to LINQ. In some scenarios ESQL offers more functionality than LINQ.

Executing custom sql including complex select joins using entity framework 4

My application currently uses ado.net to access the database.
It allows users to configure dashboards by passing custom sql. The custom sql includes joins on multiple tables and the columns of every table are included in the result.
We are migrating from ado.net to entity framework 4.
How do I execute the same queries using entity framework?
Also other code in the application requires firing custom complex join queries on the database. This is done by developers.
Yes you can use inline queries and even stored procedue in entityframework
see example for query http://msdn.microsoft.com/en-us/library/bb738451
see example for stored procedure http://msdn.microsoft.com/en-us/library/bb896334.aspx
If you have dynamic queries you cannot execute them through EF. EF works in strongly typed manner so it expects that you created the type with correct properties (with correct types) at design time (you can create the type at runtime as well but it requires you to create dynamic assembly, emit IL, etc.).
Use your old approach for this type of queries.

Prepared statements in Entity Framework

How can I run a prepared statement against a database and have the returned rows automatically attach to the entity framework data context? Essentially, how can I achieve the same functionality of ObjectContext.Execute except using a prepared SQL command.
Use ObjectContext.Translate.

Categories