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

查看:207
本文介绍了使用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?

需要做什么来连接到SQL Server与EF6代码 - 第一?我确保EntityFramework.dll和EntityFramework.SqlServer.dll在应用程序文件夹中都可用。我甚至在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天全站免登陆