How To Fix “Reference To Type ‘SqlConnection’ Claims Defined In ‘System.Data’, But It Could Not Be Found”

In this blog, I will show you how to fix the following error:
 
“Reference to type ‘SqlConnection’ claims is defined in ‘System.Data’, but it could not be found”.

 Are you anxious? Then go to “THE SOLUTION."

SPOILER

It’s a little bit painful but it can be solved. 

You’ll be able to use .NET Framework Desktop App or ASP.NET inherit/use SqlConnection from netstandard2.

PROBLEM

If you are getting this message: “Reference to type ‘SqlConnection’ claims it is defined in ‘System.Data’, but it could not be found”; it is because you are trying to use the .NET Standard component directly from the .NET Framework (Desktop or ASP.NET).

Why is that you cannot use the object SqlConnection from .NET Framework hosted inside NetStandard 2.0?

 

ABOUT MY PROJECT

I do have an application, that is ASP.NET and WinForms, using .NET Framework 4.7.1. The core of the code is in assemblies that are shared between both, using System.Web and System.WebForms.

I enjoy migrating to new technologies to see how far they can go. And in this case, my hope was to use less memory on the server side.

I started using .NET Standard 1.6, but I didn’t feel comfortable to deploy it on production. And when 2.0 was released with the SQL Client, I imagined that it was the time. However, I didn't have time at that moment, so I started to migrate a month ago. Well, it took only two days.

I’ve decided to write this post because there isn't a good solution for my purpose. And, I made this on my own.

 

ATTACHED PROJECTS

I’ve attached two projects, one with the error and other with the error fixed, so you can analyze them both.

THE SOLUTION 

STEPS

Create an intermediate library in .NET Standard 2.0 to open the Database Connection.

All the code to open connections must be in .NET Standard 2.0. 

Install all the projects that use System.Data, the NuGet System.Data.SqlClient 4.5.1. or later.

 

Change all the code from .NET Framework libraries that use System.Data to the .NET Standard 2.0.

In the main program/ASP.NET, start the connection with this.

  1. using(var oCnn = Configurations.GetConnection())  
  2. {  
  3. …  
  4. }  

Why is this necessary?

If you don’t use “var”, your main code will make a reference to the System.Data on the current .NET Framework project. But with “var”, the compilation will refer to “netstandard2” System.Data.SqlClient at the compiling time.

That's all!

SAMPLE

See attached projects. 

CONCLUSION

I always migrate to new technologies and make modern ways to do the same job more secure, better, faster, and modern to the end users. I calculate there will be a long time before the technology gets deprecated,  and I avoid using less than the 3rd version - I made an exception for this case, but only because I knew that the roadmap for .NET Standard 3.0 will be supporting WinForms. So I think it's good to refactor the codes to make it updated.

In these samples, I have used .NET Framework 4.7.1 and C# 7.2 (to use: “in” SqlConnection).