使用 EF6 连接到 SQL Server [英] Connecting to SQL Server with EF6

查看:25
本文介绍了使用 EF6 连接到 SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 EF5 之前,为了连接到 SQL Server 2012,我只需要指定一个如下所示的连接字符串:

Up to EF5, in order to connect to SQL Server 2012, all I needed to to is specify a connection string that looks something like this:

Data Source=.SqlExpress;Initial Catalog=MyDatabase;Integrated security=True;MultipleActiveResultSets=True

此方法不适用于 EF6,出现异常

This method is not working with EF6, giving exception

没有为System.Data.Odbc"ADO.NET 提供程序找到实体框架提供程序.确保提供程序已在应用程序配置文件的entityFramework"部分注册

No Entity Framework provider found for 'System.Data.Odbc' ADO.NET provider. Make sure the provider is registered in the 'entityFramework' section of the application config file

我根本没有使用 app.config 文件,我将上面的连接字符串传递给 MyContext 构造函数.为什么它完全尝试使用 Odbc 提供程序,而不是使用 System.Data.SqlClient?

I am not using app.config file at all, I am passing above connection string to MyContext constructor. WHy is it trying to use Odbc provider at all, and instead not using System.Data.SqlClient?

使用 EF6 代码优先连接到 SQL Server 需要做什么?我确保 EntityFramework.dll 和 EntityFramework.SqlServer.dll 在 Application 文件夹中都可用.我什至在 WPF 项目中添加了 EF6 nuget 包 v6.0.0.1,尽管它不直接使用 EF 库,并确保将自动创建的 App.Config 文件(由 nuget)复制到 Application (Debug) 文件夹 - 仍然没有成功.

What needs to be done to connect to SQL Server with EF6 code-first? I made sure that EntityFramework.dll and EntityFramework.SqlServer.dll are both available in Application folder. I have even added EF6 nuget package v6.0.0.1 in WPF project, although it does not use EF library directly, and making sure that automatically created App.Config file (by nuget) is copied to Application (Debug) folder - still no success.

我也尝试在代码中手动设置提供者:

I have also tried to setprovider manually in code:

public class OeeCoachConfiguration : DbConfiguration
{
    public OeeCoachConfiguration()
    {
        SetProviderServices("System.Data.SqlClient",
            System.Data.Entity.SqlServer.SqlProviderServices.Instance);
    }
}

仍然没有成功.我的项目结构如下(简化):

Still no success. My Project structure is as follows (simplified):

WPF 项目 - 没有对 EF 的引用(也尝试添加 EF 引用)

WPF project - does not have reference to EF (also tried adding EF reference)

ViewModel 类库 - 不引用 EF

ViewModel class library - does not have reference to EF

模型类库 - 引用了 EF 库(两个 dll)

Model class library - has reference to EF library (both dlls)

数据类库 - 引用了 UI 库(两个 dll).

Data class library - has reference to UI library (both dlls).

我正在使用 CodeFirst 方法,并且此设置在 EF5 上没有任何问题.非常感谢任何帮助.

I am using CodeFirst approach, and this setup works without any problem with EF5. Any help is greatly appreciated.

推荐答案

对我来说,这个错误是通过删除 Glimpse.ADO 包解决的

For me this error was resolved by removing the Glimpse.ADO package

2013 年 12 月 20 日编辑我相信 Glimpse.ADO 包现在支持 EF6,但我还没有测试过.

Edit December 20, 2013 I believe the Glimpse.ADO package now supports EF6, but I have not tested it.

这篇关于使用 EF6 连接到 SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆