简体   繁体   中英

Unable to use nuget packages

I have written some code that uses the following packages:

using System;
using System.IO;
using System.Reflection;
using Microsoft.Azure.WebJobs;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;

But when I compile the code there is a few errors related to the following code, that I suppose it is written by the "Microsoft.Data.SqlClient" package:

using Microsoft.Data.Tools.Schema.Sql.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
namespace GetDatafromBlob0201
{
    [TestClass()]
    public class SqlDatabaseSetup
    {
        [AssemblyInitialize()]
        public static void InitializeAssembly(TestContext ctx)
        {
            // Setup the test database based on setting in the
            // configuration file
            SqlDatabaseTestClass.TestService.DeployDatabaseProject();
            SqlDatabaseTestClass.TestService.GenerateData();
        }
    }
}

Where "using Microsoft.VisualStudio.TestTools.UnitTesting" throws an error. I have tried installing the NuGet package, but that didn't solve the error.

These are the errors I get: Errors after compiling

Mostly CS0246. The last one indicates that the reference could not be used.

I am also noticing a warning sign under my proyect, on "Packeges" and also on "Assembly":

  1. The one on Assembly its on Microsoft.Data.Tools.Components, Version= 16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL. It doesn't give me details on the warning.

  2. The one on Packages is on Microsoft.Data.Tools.UnitTest and it says: Package 'Microsoft.Data.Tools.UnitTest' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead the project target framework '.NETCoreApp,Version=v3.1'. This package may not be fully compatible with your project.

Test with your csproj file and install any nuget packages, I did not get any build errors.

These are the nuget packages which I have installed.

Microsoft.Data.SqlClient

Microsoft.Azure.WebJobs

Microsoft.Data.Tools.UnitTest

So please make sure that there is no red error lines on your code editor.

Then , close VS, delete .vs hidden folder under the solution folder, bin and obj folder.

After that , restart VS and your project, then, run update-package -reinstall command under Tools --> Nuget Package Manager --> Package Manager Console

Update 1

I think you have created a MSTest test project(net core) project rather than a unit test project(net framework) project.

In my side , I used unit test project(net framework) , and all work well.

Actually , some of your nuget packages are for net framework . That is what the warning NU1701 did. But you can ignore that warning since it is normal that you have install the package into net core rather than net framework. And the package can be used under net core. And it is just a little warning rather than a error.

Due to that, update-package -reinstall cannot work for new-sdk project(your situaiton) and only for packages.config with net framework .

Maybe try these:

1)Besides , I think Microsoft.Data.Tools.Components.dll is from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\Extensions\Microsoft\SQLDB . You have used some dlls from there. And due to some situations, these do not work and you should readd them on your current environment.

And first, please delete any references from here:

在此处输入图像描述

Then , readd these dlls from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\Extensions\Microsoft\SQLDB to check if your project still have these errors.

Or you could remove Microsoft.Data.Tools.Components.dll from there to fix it.

If these do not help, maybe try nuget packages like what I said above:

1) clean all nuget caches first or just delete all files under C:\Users\xxx\.nuget\packages

2) to remove that warning, please add this xml node under csproj file of the unit test project.

在此处输入图像描述

在此处输入图像描述

3) Or you could try to use unit test project(net framework) project and then add your code there and I ensure that could work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM