Executing custom sql including complex select joins using entity framework 4 - c#

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.

Related

How to fetch data in .NET Core WEP API project from MS SQL, using existing SQL queries?

I have a .NET Core 3.1 Web API project and I have an existing MS SQL database filled with data.
In my project I need to fetch the data from the database and to return it as results via the API endpoints. I already have the SQL queries for fetching the data in the format:
SELECT <fields>
FROM <table>
WHERE <condition>
And I wonder, what is the best way to get to fetch the data from the database and in the same time to be able to maximum reuse the SQL code that I already have.
I was considering options like:
using Entity Framework Core as a framework (but then, I already have the SQL code and I feel that using the entities with LINQ queries will only result with repetitive work, as I will have to translate my SQL queries into the LINQ queries);
using StoredProcedures and somehow call them (so that I can copy paste my SQL code inside of the procedures and on that way to maximum reuse them). Still I am not sure what is the best way to call / write stored procedures, do I need to use any framework;
using some combined approach, where I will actually use the StoredProcedures but I will call them via Entity Framework Core;
The above points were some conclusions based on my research, as I am not experiences with .NET Core development in general. So any advices will be helpful.
I would suggest having a look at Dapper. It's a Micro-ORM that easily enables you to query your database with SQL, and then mapping the result to .NET objects.

In asp.net are the queries generated at runtime?

I'm new to c # and asp.net/entity framework and in general to the concept of compiled language.
I wanted to know if, in asp.net, using the model and related functions to interact with the database, following a request the queries (which use variable values passed during the request) are generated at runtime or are they somehow precompiled together with the program itself?
What I would like to understand is if in an asp.net project using raw queries is still faster than using the model and therefore the or, in this case, nothing changes.
.net can make use of many libraries to connect to database. For example, you can use System.Data.SqlClient to connect to sql server database and run sql command against it. Entity framework is another library which can be used to connect to databases. This is a Object Relational Mapper (ORM) which generates queries dynamically, but you can also run sql statements against a database if you wish to using Entity Framework.

Entity framework run direct query

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.

create entities for a dynamically created database in SQL Server

I have an application where I create database's dynamically in a SQL Server using Server Management Objects through my application.
I want to use the Entity Framework to access this database when it has been created, is this possible? As I can not generate Entity classes from a database in VS. I do have the structure of the database of course.
So is it possible to create the Entity classes manually and is that a do-able task?
Yes, it's completely possible. You can even manipulate the generated code if you want.
What you might want to take a look is the EDMX XML specification.
In that file you specify the underlying database, views, functions, procedures and the like, as well as the desired objects. Take a look at MSDN in order to have more information.
Paulo is right for EF 1 (+1). For EF 4, I'd suggest using code-only modeling instead.

NHibernate to dynamically create/alter/delete tables

I know NHibernate is an ORM and it isn't normally used to create tables but I also know that NHibernate is able to create an entire database given some mappings.
I would like to know if there is an obscured API that I could use to dynamically create/alter/delete tables. I could do it with ADO.Net but I would like to abstract the code for creating tables for different databases (MS SQL, MySQL, etc.)
Precision 1: The problem with CreateSQLQuery is that I would have to rewrite the method for creating a table for different SQL servers (MS SQL, MySQl, etc.) It has no advantages over ADO.Net. When NHibernate generates the database from mappings it generates for any SQL servers... that is what I'm looking for. What is the code that is executed when NHibernate generates a database from mappings... is this code available/public?
Yes you can :)
You create dynamically a Type corresponding to the class to be mapped. And then generate a mapping.
The important classes to look in NH source code are in the Nhibernate.Mapping namespace : PersistentClass, RootClass.
Here is a sample :
https://nhibernate.info/blog/2008/11/16/mapping-source-how-map-a-class-without-use-nothing.html
I have used this "API" to generate dynamically Tables.
There isn't a specific API to dynamically create/alter/delete tables. Depending on what you need to do and when you want these actions to happen you have the following options:
Use the tag in the xml mapping files to perform whatever action you want on the database just after your schema is generated
Use named queries and in the mapping files to create SQL statements to run from your code. (I have never tried these with create/alter/delete table commands but its worth to try).
Use the Session.CreateSQLQuery() method to execute a native SQL command. (Again as the previous option I have never tried it with create/alter/delete table commands but I believe its worth to try).
You could use the schema object.
You need to configure NHibernate and using the schema object call create. the two boolean values will either drop the database and recreate it or just output it to the console.
VB.Net
Public Sub CreateDatabaseSchemaFromMappingFiles()
Dim cfg As New NHibernate.Cfg.Configuration()
cfg.Configure()
Dim schema As New NHibernate.Tool.hbm2ddl.SchemaExport(cfg)
schema.Create(True, False)
End Sub

Categories