C# 带有 OleDB 连接的实体框架 - 我只是普通的疯子吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1132720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:59:52  来源:igfitidea点击:

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

c#entity-frameworkms-access

提问by fran

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

我正在试验实体框架,我想连接到 Access 2007 数据库。

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

以下代码的灵感来自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.

EntityConnectionStringBuilder 告诉我它不支持 DataSource 属性。我可以很好地连接 ADO.net,所以我知道路径和提供者是正确的。

Is this just the complete wrong approach?

这只是完全错误的方法吗?

采纳答案by Alex James

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

您用于构建 EF 连接字符串的方法是正确的。

BUT...

但...

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

实体框架仅适用于支持称为提供者服务的提供者(即 SqlClient)。

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).

OleDB 提供程序不支持“提供程序服务”,因此您不能将 EF 与 OleDb 一起使用(除非您可以找到支持 EF 的第三方 OleDb 提供程序)。

Hope this helps

希望这可以帮助

Alex

亚历克斯

(Entity Framework Team, Microsoft)

(实体框架团队,微软)

回答by MusiGenesis

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

我不确定你是否有棒的两端。:)

Check out this exampleinstead. 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 ProviderConnectionStringproperty (among other properties).

请查看此示例。您的代码可能存在其他问题,但当您需要设置其ProviderConnectionString属性(以及其他属性)时,您似乎正在设置实体构建器的 ConnectionString 属性。

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.

在我看来,对于称为“连接字符串构建器”的东西,ConnectionString 属性应该是只读的(不是)。我猜它也打算兼作连接字符串解析器。

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!

编辑:我刚刚再次查看了您的代码,我认为您所要做的就是将 ConnectionString 更改为 ProviderConnectionString。毕竟你可能有棍子!

回答by Shiraz Bhaiji

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

要构建连接字符串,请在桌面上创建一个名为 a.udl 的文件

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

双击它,应该会打开一个 UI。按照向导,测试连接。

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

然后关闭 UI,用记事本打开文件,你就有了你的连接字符串。

EDITIt 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.

编辑您收到此错误可能是因为您缺少参考。实体框架使用扩展方法。因此,它可能会编译但仍然无法正常工作。

回答by bubi

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

OleDb 可能不适用于 EF,但如果您以这种方式设置 entityBuilder 和 oledbConn,则初始化工作正常。

        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();