C# 如何使用实体Framewok(v6)库?

C# 如何使用实体Framewok(v6)库?,c#,entity-framework,C#,Entity Framework,我可以制作一个实体框架控制台应用程序,在Northwind数据库中显示客户 然而,当我试图在一个单独的类库中创建Northwind实体框架,而这个类库由一个单独的子项目中的简单C#console应用程序使用时,我遇到了一些问题 我读 通过将app.config中的连接字符串从库复制到控制台模式程序目录,并添加对我的northwind EF类库的引用,观察并遵循指示 在控制台程序中尝试创建dbcontext变量时,我仍然遇到此异常: An unhandled exception of type '

我可以制作一个实体框架控制台应用程序,在Northwind数据库中显示客户

然而,当我试图在一个单独的类库中创建Northwind实体框架,而这个类库由一个单独的子项目中的简单C#console应用程序使用时,我遇到了一些问题

我读 通过将app.config中的连接字符串从库复制到控制台模式程序目录,并添加对我的northwind EF类库的引用,观察并遵循指示

在控制台程序中尝试创建dbcontext变量时,我仍然遇到此异常:

An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll

Additional information: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
在谷歌搜索之后,我尝试添加以下代码:

public abstract class BaseDomainContext : System.Data.Entity.DbContext{
    static BaseDomainContext() {
        var ensureDLLIsCopied = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
    }
}
现在的问题是,我在“SqlServer”下面有一条红色的曲线——显然,在我的System.Data.Entry副本中没有这样的名称空间。我刚用nuget升级到EF 6。 有人能帮我解决这个问题吗?
谢谢
齐格弗里德

2014年3月30日星期日: 忘了提及:我正在Windows 8.1上使用Visual Studio 2013。这是我的控制台模式演示程序中的app.config,其中大部分是从类库app.config复制的。现在我想起来了,谁在寻找那个连接字符串?是类库还是我的控制台模式演示程序?连接字符串的名称应该是什么

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <connectionStrings>
    <add name="NorthwindEntitiesLib" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=KM;initial catalog=Northwind;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

2014年3月31日星期一: VS 2012一切正常。我只需添加对类库的引用,将库App.config中的大部分App.config文件剪切并粘贴到控制台模式App.config,一切正常。我们可以从中得出什么结论?看起来VS2013中EF 6有一个bug。有人能帮我解决这个问题吗


本:我试过你的建议,从你发布的XML判断,看起来你在使用EF5。我刚升到6级。在VS 2013中,我仍然会遇到相同的错误。

它位于System.Data.dll中,即
System.Data.SqlClient

请确保您对此有参考


你能发布你的连接字符串吗?

这一个对我很有用-我刚刚创建了一个新项目,并在项目中添加了一个新的.edmx文件,名为:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <connectionStrings>
    <add name="NorthwindEntitiesLib" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=KM;initial catalog=Northwind;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>
using (YourDbContext db = new YourDbContext(){///}
这将调用上下文的无参数构造函数,该上下文使用app.config中的默认连接字符串设置。亲自看看构造函数,看看什么是自动生成的,以及使用了什么连接字符串

<configuration>
    <configSections>
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </configSections>
    <connectionStrings>
        <add name="NorthwindEntitiesLib" connectionString="metadata=res://*/App_Code.Model.csdl|res://*/App_Code.Model.ssdl|res://*/App_Code.Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=KM;initial catalog=Northwind;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>
     <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
     </entityFramework>
</configuration>

这是我的app.config——其中大部分是从类库app.config复制的。使用VS2012,它也适用于我。我很遗憾我用nugget将我的VS2013项目从EF5升级到EF6。当我将您的代码粘贴到我的VS2013/EF6项目中时,它不起作用,因为您使用的是EF5。当我将版本号编辑为6时,它仍然不起作用。有没有办法在VS2013中卸载EF6并回滚到EF5?