No Entity Framework provider found for the ADO.NET

2019-01-01 09:35发布

After downloading the EF6 by nuget and try to run my project, it returns the following error:

No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

enter image description here

30条回答
临风纵饮
2楼-- · 2019-01-01 10:35

The startup project that references the project where Entity Framework is being used needs the following two assemblies in it's bin folder:

  • EntityFramework.dll
  • EntityFramework.SqlServer.dll

Adding a <section> to the <configSections> of the .config file on the startup project makes the first assembly available in that bin directory. You can copy this from the .config file of your Entity Framework project:

<configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>

To make the second .dll available in the bin folder, although not practical, a manual copy from the bin folder of the Entity Framework project can be made. A better alternative is to add to the Post-Build Events of the Entity Framework project the following lines, which will automate the process:

cd $(ProjectDir)
xcopy /y bin\Debug\EntityFramework.SqlServer.dll ..\{PATH_TO_THE_PROJECT_THAT_NEEDS_THE_DLL}\bin\Debug\
查看更多
浅入江南
3楼-- · 2019-01-01 10:35

I had a related issue when migrating from a CE db over to Sql Server on Azure. Just wasted 4 hrs trying to solve this. Hopefully this may save someone a similar fate. For me, I had a reference to SqlCE in my packages.config file. Removing it solved my entire issue and allowed me to use migrations. Yay Microsoft for another tech with unnecessarily complex setup and config issues.

查看更多
高级女魔头
4楼-- · 2019-01-01 10:38

Add this function

private void FixEfProviderServicesProblem()

to database context class in the library class and the missing DLL EntityFramework.SqlServer.dll will be copied to the correct places.

namespace a.b.c
{
    using System.Data.Entity;

    public partial class WorkflowDBContext : DbContext
    {
        public WorkflowDBContext()
            : base("name=WorkflowDBConnStr")
        {
        }

        public virtual DbSet<WorkflowDefinition> WorkflowDefinitions { get; set; }
        public virtual DbSet<WorkflowInstance> WorkflowInstances { get; set; }
        public virtual DbSet<EngineAlert> EngineAlerts { get; set; }
        public virtual DbSet<AsyncWaitItem> AsyncWaitItems { get; set; }
        public virtual DbSet<TaskItem> TaskItems { get; set; }
        public virtual DbSet<TaskItemLink> TaskItemLinks { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
        }

        private void FixEfProviderServicesProblem()
        {
            // The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer'
            // for the 'System.Data.SqlClient' ADO.NET provider could not be loaded. 
            // Make sure the provider assembly is available to the running application. 
            // See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
            var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
        }
    }
}

.

查看更多
孤独总比滥情好
5楼-- · 2019-01-01 10:38

I tried almost all the above and nothing worked.

Only when I set the referenced DLLs in the Default Project EntityFramework and EntityFramework.SqlServer properties Copy Local to True did it start working!

查看更多
浪荡孟婆
6楼-- · 2019-01-01 10:38

I had the identical exception thrown. I included

using System.Data; 
using System.Data.Entity;

and everything is back to working again ..

查看更多
人间绝色
7楼-- · 2019-01-01 10:39

Also, make sure you startup project is the project that contains your dbcontext (or relevant app.config). Mine was trying to start up a website project which didnt have all the necessary configuration settings.

查看更多
登录 后发表回答