Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework with OleDB connection - am I just plain nuts?

I'm experimenting with the Entity Framework and I want to connect to an Access 2007 database.

The following code is inspired by http://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnection.connectionstring.aspx

I suspect that I've got the wrong end of the stick...

OleDbConnectionStringBuilder oledbConn = new OleDbConnectionStringBuilder();

oledbConn.DataSource = @"..\..\..\..\Pruebas.accdb"; //yep Access 2007!

EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder ();
entityBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
entityBuilder.ConnectionString = oledbConn.ToString();
EntityConnection ec = new EntityConnection(entityBuilder.ToString());
ec.Open();
ec.Close();

The EntityConnectionStringBuilder tells me that it doesn't support the DataSource property. I can connect fine with ADO.net so I know that the path and the provider are correct.

Is this just the complete wrong approach?

like image 613
fran Avatar asked Jul 15 '09 17:07

fran


People also ask

What is OLE DB connection used for?

Object Linking and Embedding Database (OLE DB) is a connectivity method similar to Open Database Connectivity (ODBC) and uses the same core application programming interface (API) to help bridge communication between client applications and a variety of data sources.

What is OLE DB connection string?

The . NET Framework data provider for OLE DB connects to an OLE DB data sources through the OleDbConnection object. The OLE DB provider connection string is specified using the ConnectionString property of the OleDbConnection object.

Does Entity Framework use OLE DB?

EF cannot use OLE DB directly because an EF provider have to translate c# into specific SQL syntax (OLE DB is a connection to several DBMSs with several SQL syntaxes).

What is OLE DB connection class?

An OleDbConnection object represents a unique connection to a data source. With a client/server database system, it is equivalent to a network connection to the server. Depending on the functionality supported by the native OLE DB provider, some methods or properties of an OleDbConnection object may not be available.


1 Answers

The approach you are using to build the EF connection string is correct.

BUT...

The Entity Framework only works with Providers (i.e. SqlClient) that support something called provider services.

The OleDB provider doesn't support 'Provider Services' so you can't use EF with the OleDb (unless you can find a third party OleDb provider with support for EF).

Hope this helps

Alex

(Entity Framework Team, Microsoft)

like image 130
Alex James Avatar answered Sep 17 '22 12:09

Alex James