EF6报错0152:未找到System.Data.SqlClient提供程序,求排查遗漏点
Hey there, I feel your pain—you’ve tried the usual fixes (reinstalling EF, checking versions, tweaking connection strings) but still hit this wall. Let’s go through some often-overlooked steps that might resolve this:
Verify your config has the SqlClient provider registered
Even if you’ve set theproviderNamein your connection string, EF6 needs explicit registration of the provider in theentityFrameworksection of your startup project’s app.config/web.config. Add this if it’s missing:<entityFramework> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework>This tells EF exactly where to find the SqlServer provider logic.
Ensure the
EntityFramework.SqlServerassembly is referenced and copied locally
EF6 splits the SqlServer provider into a separate NuGet package. Even if you installed EntityFramework, double-check thatEntityFramework.SqlServeris also installed (runInstall-Package EntityFramework.SqlServerin Package Manager Console if needed). Then, in your project references, right-clickEntityFramework.SqlServer, go to Properties, and make sure Copy Local is set toTrue—this ensures the DLL is deployed with your app.Double-check your EntityClient connection string format
When usingproviderName="System.Data.EntityClient", your connection string needs to include metadata references (to your CSDL/SSDL/MSL files) and explicitly specify the underlying SqlClient provider. A correct example looks like:<connectionStrings> <add name="MSLEntities" connectionString="metadata=res://*/YourModel.csdl|res://*/YourModel.ssdl|res://*/YourModel.msl;provider=System.Data.SqlClient;provider connection string="data source=YourServer;initial catalog=YourDB;integrated security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" /> </connectionStrings>Make sure the
metadatapaths match your model files, and theproviderattribute inside the connection string is set toSystem.Data.SqlClient.Confirm your project targets a supported .NET Framework version
EF6 requires at least .NET Framework 4.0, but issues can crop up if you’re targeting an older or incompatible version (like .NET Core, though EF6 is for full .NET Framework). Go to your project properties → Application → Target framework, and ensure it’s set to .NET Framework 4.0 or higher (4.5+ is recommended).Check if your config is in the right project
If your data access code is in a class library, remember that class library app.config files aren’t used at runtime—all configuration must live in the startup project’s config file (the one that launches your app, e.g., a web project’s web.config or console app’s app.config). If you made changes to the class library’s config, copy those settings to the startup project’s config.Rule out version conflicts
Sometimes third-party NuGet packages or references might pull in an older version of EF, causing conflicts. RunGet-Packagein Package Manager Console to list all installed packages, and ensure every EF-related package (EntityFramework, EntityFramework.SqlServer) is on version 6.0. If you see mixed versions, uninstall all EF packages and reinstall them cleanly.
Give these steps a try—one of them is almost certainly the missing piece here!
内容的提问来源于stack exchange,提问作者Priyanka

