npgsql LINQ creates SQL Server sql syntax - linq

I'm trying to use LINQ with Npgsql 2.0.11 in a .NET v3.5 project. I'm trying my first simple query from a data table, and I've found that the syntax sent to Postgresql is SQL Server syntax, not Pgsql syntax, causing the server to throw a syntax error.
I have added the factory generation to the project's App.config as suggested by the documentation:
<system.data>
<DbProviderFactories>
<add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql, Version=2.0.11.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7"/>
</DbProviderFactories>
</system.data>
Here is a snippet:
DbProviderFactory factory = DbProviderFactories.GetFactory("Npgsql");
DbConnection connection = factory.CreateConnection();
connection.ConnectionString = "Server=mydbhost.example.com;UserId=postgres;Database=postgres";
table = new DataContext(connection).GetTable<Project.Model.MyEntity>();
I've found that factory is an instance of Npgsql.NpgsqlFactory (seems right), and connection is an instance of Npgsql.NpgsqlConnection. All that seems good. However, when I attempt to GetTable, the SQL syntax generated contains square brackets and various other SQL Server specific syntax.
What could be missing?

DataContext
DataContext is LinqToSql. LinqToSql is for SqlServer only.
Perhaps you meant to use LinqToEntities and ObjectContext?

If one wants to use LINQ to SQL for RDBMS other than Microsoft SQL Server, a third-party assembly is required. DBLinq is a project that provides LINQ to SQL functionality for other (open-source) databases.
http://code.google.com/p/dblinq2007/
Npgsql can be configured as a db provider for Entity Framework following the documentation on this Npgsql blog:
http://npgsql.com/index.php/2009/08/how-to-set-up-entity-framework-npgsql-part-1/

Related

Migrate entity framework core db first sql server to oracle

I have a .net core project where i am using entity framework db first connecting to sql server which is working fine, now i have similar set of tables in oracle can i just run the project by changing connection string and provider to oracle? Or any major rework is needed to make this work?
You need to regenerate the DbContext mappings using reverse engineering

Confused with Ado .net, Entity Framework,LINQ

I know ASP .Net and ADO .NET But I am really confused with this Entity Framework and LINQ.
Can We Use Entity Framework to Interact with Db without like ADO .NET.Are all these three (ADO ,Entity framework,LINQ) is alternative to each other ?
Please guide
Here is my best explanation (after hours of confusion!):
ADO.NET = Part of the .NET framework enabling you to easily access data and data services like Microsoft SQL Server
LINQ (Language-integrated query) = Enables you to query data from within .NET programming languages
LINQ has been integrated in various areas of the .NET framework including:
LINQ to Objects
LINQ to XML
LINQ to ADO.NET
LINQ to ADO.NET = enables you to query over any enumerable object in ADO.NET by using LINQ
There are 3 LINQ to ADO.NET technologies:
LINQ to DataSet = enables you to write queries against the DataSet (a class that represents an in-memory version of a DB)
LINQ to SQL = enables you to write OO queries in your .NET project that targets SQL databases. It translates LINQ queries into SQL statements. This is no longer supported by Microsoft due to the great overlap in functionality with the Entity Framework (EF).
LINQ to Entities = Almost anything you can do in LINQ to SQL can be done in LINQ to Entities. Enables developers to write queries against the Entity Framework(EF) conceptual model using VB or C#.
EF is an Object Relational Mapper (ORM) that supports the development of data-oriented software applications. With EF, you can take a bunch of database objects like tables and turn them into .NET objects that you can access in your code. You can then use these objects in queries or use them
directly in data-binding scenarios. EF also enables you to do the reverse: design an object model first and then left EF create the necessary database structure for you.
Source: Beginning ASP.NET 4.5.1, Microsoft Documentation
I assume that by LINQ you actually mean LINQ to SQL.
EF (Entity Framework) and L2S (LINQ to SQL) are object-relational
mappers that use ADO.NET internally, encapsulating most of its functionality. So if you use either, you'd still be using ADO.NET.
All three are viable options for building the data access layer or your code, with their own strengths and weaknesses. Have a look at this post for more info:
Entity Framework VS LINQ to SQL VS ADO.NET with stored procedures?

Using ASP.NET Identity with an Oracle database and thus Entity Framework 5

Is there any chance to use ASP.NET Identity with Entity Framework while connecting to an Oracle database?
The challenge seems to be some version conflicts:
The Oracle Data Provider for .NET only targets Entity Framework 5 but not Entity Framework 6.
The NuGet package Microsoft.AspNet.Identity.EntityFramework targets EF6, not EF5.
It's not the code first feature that troubles me. It's getting the connection up and running and reading my user identity data from the Oracle database that bugs me.
Any pointers are much appreciated.
The solution is, of course, in implementing an UserStore class for the IUserStore (and IUserPasswordStore and IUserClaimStore and whatever one needs) interfaces.
Therein, it's just plain EF5 code that queries and updates the ASP.NET Identity tables.
Using this, the UserManager works just fine for accessing all identity data.

how to use Code first approach for oracle

I succeeded in code first approach for SQL in mvc4 application. But when I want to apply code first approach for Oracle then i am getting below message:
No Entity Framework provider found for the ADO.NET provider with invariant name
'Oracle.DataAccess.Client'. Make sure the provider is registered in the
'entityFramework' section of the application config file
Right now I am using Entity framework 6. Please let me know what is the simple step to create database using code first approach for ORACLE. Which "ddl" is required? What is web config setting?
You need to specify a provider for your Oracle DB, and there is no native provider,you need a third party one.
Here you have an "official" list of all the entity framework providers for different databases. Note that there is one from Oracle, that can also be installed as a Nuget package.
NOTE from Oracle docs:
Entity Framework 6 is not supported at this time. Some Visual Studio versions may default to Entity Framework 6. If so, set your .NET project to use an earlier version of Entity Framework.
DevArt dot connect supports all versions of EF.
So, to use the ODP you need to downgrade to EF5, or purchase dotConnect.
The details for configuring your web.config, or app.config can be found here: Deploying and Configuring ODP.NET to work without installation with Entity Framework
Starting with Oracle Data Access Components (ODAC) 12c Release 3 (12.1.0.2.1), ODP.NET supports Code First and Code First Migrations from Entity Framework 6. ODP.NET provides the data access to store the EF model inside the Oracle Database and evolve it.
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/CodeFirst/index.html#section1

How can I use DataContext class to establish a connection to Oracle Database?

Is it possible to use the DataContext class to connect to an Oracle DB?
I am reading some examples on MSDN
I need an example where the context is used to connect to Oracle.
DataContexts are specific to LINQ-to-SQL and thus SQL Server. If you want an ORM for Oracle, you need to look at alternatives like the Microsoft Entity Framework or NHibernate.

Resources