10

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?

fran
  • 365
  • 1
  • 3
  • 19

4 Answers4

17

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)

Alex James
  • 20,874
  • 3
  • 50
  • 49
  • 2
    Thanks for that. I _was_ just being nuts after all :) – fran Jul 16 '09 at 10:01
  • Is that still true in 2018? – Daniel Möller Apr 18 '18 at 16:08
  • 2
    @DanielMöller see https://github.com/bubibubi/JetEntityFrameworkProvider – intrixius Sep 28 '18 at 14:42
  • @intrixius is there any jet provider for EF Core? – Husam Ebish Nov 16 '22 at 08:40
  • @HusamEbish, no offence, but you should upgrade your search skills a bit ;-) , when you click on my link, and you go to the bubibubi main page, you will see that also a Core version exists: https://github.com/bubibubi/EntityFrameworkCore.Jet (PS I am not bubibubi and have no connection with him/her/...) – intrixius Nov 16 '22 at 13:12
  • @intrixius thx for the note, in fact, if I hadn't bothered to search, I wouldn't have asked you. Already tried this package and many others and it didn't work with EF 6, the package you mentioned is compatible with EF Core >= 3.1.22 && < 5.0.0 – Husam Ebish Nov 16 '22 at 13:58
1

I'm not sure you have either end of the stick. :)

Check out this example instead. There might be other issues with your code, but it looks like you're setting the entity builder's ConnectionString property when you need to be setting its ProviderConnectionString property (among other properties).

It seems to me that for something called a "connection string builder", the ConnectionString property should be read-only (it's not). I guess it's intended to also double as a connection string parser.

Edit: I just looked at your code again, and I think all you have to do is change ConnectionString to ProviderConnectionString. You may have the stick after all!

MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
  • I had already tried that and then it asks for MetaData to be set. I'm not sure how to generate that. – fran Jul 15 '09 at 17:53
  • 1
    Beats me, too. I'm guessing the metadata for Access is the location of the .mdb file. MSDN really sucks sometimes (this is one of those times). I'm pretty sure the whole thing is auto-generated from code comments, so the more unusual stuff often goes completely unexplained. If I were doing this, I'd skip the whole entity string builder thing. – MusiGenesis Jul 15 '09 at 18:02
  • Shiraz' advice is better. :) – MusiGenesis Jul 15 '09 at 18:19
1

To build your connection string create a file on your desktop called a.udl

Double click on it, should open a UI. Follow the wizard, test the connection.

Then close the UI, open the file with notepad, and you have your connection string.

EDIT It may be that you are getting this error because your are missing a refernce. Entity Framework uses extension methods. Therefore, it might compile but still not work.

Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
  • 1
    I think you need a "voilà" in there somewhere. This is a handy trick. – MusiGenesis Jul 15 '09 at 18:21
  • Wow! I didn't know you could generate conenction strings like that. Unfortunately the EntityConnection doesn't support the "Data Source" property. (or am I just being an idiot) – fran Jul 15 '09 at 18:29
0

Probably OleDb does not work with EF but the initialization works fine if you set the entityBuilder and the oledbConn in this way.

        OleDbConnectionStringBuilder oledbConn = new OleDbConnectionStringBuilder();
        oledbConn.Provider = "Microsoft.Jet.OLEDB.4.0";
        oledbConn.DataSource = @"C:\Users\Utente\Documents\visual studio 2013\Projects\How to implement the1\Debug\Test.mdb";

        EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
        entityBuilder.Provider = "System.Data.EntityClient";
        string connectionString = string.Format("metadata=res://*/School.csdl|res://*/School.ssdl|res://*/School.msl;provider=System.Data.OleDb;provider connection string='{0}'", oledbConn);
        entityBuilder.ConnectionString = connectionString;
        EntityConnection ec = new EntityConnection(entityBuilder.ToString());
        ec.Open();
        ec.Close();
bubi
  • 6,414
  • 3
  • 28
  • 45