在通过nuget下载EF6并尝试运行我的项目后,它返回以下错误:

没有为ADO找到实体框架提供程序。NET提供程序,使用不变名称'System.Data.SqlClient'。确保提供者在应用程序配置文件的“entityFramework”部分中注册。更多信息请参见http://go.microsoft.com/fwlink/?LinkId=260882。


当前回答

而不是添加EntityFramework。您可以确保从模型/实体项目中对它进行静态引用,如下所示

static MyContext()
{
    var type = typeof(System.Data.Entity.SqlServer.SqlProviderServices);
    if(type == null)
        throw new Exception("Do not remove, ensures static reference to System.Data.Entity.SqlServer");
}

这将使构建过程将程序集与主机项目包括在一起。

更多信息在我的博客上 http://andersmalmgren.com/2014/08/20/implicit-dependencies-and-copy-local-fails-to-copy/

其他回答

而不是添加EntityFramework。您可以确保从模型/实体项目中对它进行静态引用,如下所示

static MyContext()
{
    var type = typeof(System.Data.Entity.SqlServer.SqlProviderServices);
    if(type == null)
        throw new Exception("Do not remove, ensures static reference to System.Data.Entity.SqlServer");
}

这将使构建过程将程序集与主机项目包括在一起。

更多信息在我的博客上 http://andersmalmgren.com/2014/08/20/implicit-dependencies-and-copy-local-fails-to-copy/

我得到了同样的错误,而使用实体框架6与SQL Server紧凑4.0。关于EF6实体框架提供者的MSDN的文章很有帮助。在包管理器控制台以nuget包的形式运行相应的提供程序命令可能会解决这个问题,因为nuget包也会自动向配置文件添加注册。我运行PM>安装包实体框架。SqlServerCompact来解决问题。

大家注意,两个dll EntityFramework.dll和EntityFramework.SqlServer.dll是DataAccess层库,在视图或任何其他层中使用它们是不符合逻辑的。它能解决你的问题,但不符合逻辑。

逻辑的方法是删除实体属性并用Fluent API替换它们。这是实解

正如消息显示,我们需要添加提供商System.Data.SqlClient,这就是为什么我们需要安装EntityFramework的nuget包,它有两个dll,但如果我们只开发控制台应用程序,那么我们只需要添加EntityFramework. sqlserver .dll的引用

将以下内容添加到app.config中。

 <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>