Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.NET Core CLI commands fail when wrong imports were used for first run of dotnet-restore #5458

Closed
mhenderson442 opened this issue May 21, 2016 · 22 comments
Assignees

Comments

@mhenderson442
Copy link

Updated my Visual Studio with RC 2 and created a Web API project.

Followed documentation guidelines for new application with existing database.

The following error was thrown hitting Scaffold-DbContext.

System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified. File name: 'Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
    at Microsoft.DotNet.ProjectModel.ProjectReader.ReadProject(Stream stream, String projectName, String projectPath, ProjectReaderSettings settings)
   at Microsoft.DotNet.ProjectModel.ProjectReader.GetProject(String projectPath, ProjectReaderSettings settings)
    at Microsoft.EntityFrameworkCore.Tools.DispatchCommand.<>c__DisplayClass2_0.<Create>b__0()
    at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) 
   at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)
 Could not load file or assembly 'Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified. 

Got a similiar Newtonsoft related error using CLI dotnet ef.

@lajones
Copy link
Contributor

lajones commented May 23, 2016

Can you include your project.json please?

@mhenderson442
Copy link
Author

I posted a question on Stack Overflow. It's not the complete project.json file, but all the relevant parts should be there. If you would like to see the complete file posted here I can do that too.

@lajones
Copy link
Contributor

lajones commented May 23, 2016

Can you try adding "Newtonsoft.Json": "7.0.1" to your dependencies and let me know if that works?

@mhenderson442
Copy link
Author

I tried adding Newtonsoft.Json: 7.0.1. I got errors with other dependencies were looking for version 8.03. I got messages about some things looking for 8.03, "and got 7.01 instead."

@lajones
Copy link
Contributor

lajones commented May 23, 2016

What happens if you add both?

@rowanmiller rowanmiller added this to the 1.0.0 milestone May 23, 2016
@mhenderson442
Copy link
Author

When adding 7.0.1, or both 8.0.3 and 7.0.1 I get the following warnings:

Dependency conflict. Microsoft.AspNetCore.JsonPatch 1.0.0-rc2-final expected Newtonsoft.Json >= 8.0.3 but got 7.0.1

Dependency conflict. Microsoft.AspNetCore.Mvc.ViewFeatures 1.0.0-rc2-final expected Newtonsoft.Json >= 8.0.3 but got 7.0.1

Dependency conflict. Microsoft.Extensions.Configuration.Json 1.0.0-rc2-final expected Newtonsoft.Json >= 8.0.3 but got 7.0.1

The project builds with warnings, but errors when trying to use dotnet ef or Scaffold-DbContext.

@lajones
Copy link
Contributor

lajones commented May 23, 2016

Sorry - I forgot. You have to include "dnxcore50" inside the imports section for Microsoft.EntityFrameworkCore.Tools inside the tools section. This is a workaround for a problem with CLI in RC2 and should go away in RTM.

@mhenderson442
Copy link
Author

Bummer. Thought I had a remedy.

Still getting the errors with the following:

  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": [
        "portable-net45+win8+dnxcore50",
        "portable-net45+win8",
        "dnxcore50"
      ]
    }

I have tried variations of the above, but have not solved this riddle.

@divega
Copy link
Contributor

divega commented May 23, 2016

@mhenderson442 just in case it helps, what NuGet feed are you using? Do you have a project.lock.json file?

@mhenderson442
Copy link
Author

I am using nuget.org(https://api.nuget.org/v3/index.json).

I do have a project.lock.json.

There are two references to Newtonsoft.Json 7.0.1. Excerpted below.

      "Microsoft.DotNet.ProjectModel/1.0.0-rc2-002702": {
        "type": "package",
        "dependencies": {
          "Microsoft.CSharp": "4.0.1-rc2-24027",
          "Microsoft.Extensions.DependencyModel": "1.0.0-rc2-002702",
          "Newtonsoft.Json": "7.0.1",
          "NuGet.Packaging": "3.5.0-beta-final",
          "NuGet.RuntimeModel": "3.5.0-beta-final",
          "System.Dynamic.Runtime": "4.0.11-rc2-24027",
          "System.Reflection.Metadata": "1.3.0-rc2-24027",
          "System.Runtime.Loader": "4.0.0-rc2-24027",
          "System.Runtime.Serialization.Primitives": "4.1.1-rc2-24027",
          "System.Security.Cryptography.Algorithms": "4.1.0-rc2-24027",
          "System.Threading.Thread": "4.0.0-rc2-24027",
          "System.Xml.XDocument": "4.0.11-rc2-24027"
        },


      "Microsoft.Extensions.DependencyModel/1.0.0-rc2-final": {
        "type": "package",
        "dependencies": {
          "Microsoft.DotNet.InternalAbstractions": "1.0.0-rc2-002702",
          "Newtonsoft.Json": "7.0.1",
          "System.Diagnostics.Debug": "4.0.11-rc2-24027",
          "System.Dynamic.Runtime": "4.0.11-rc2-24027",
          "System.Linq": "4.1.0-rc2-24027"
        },

@mhenderson442
Copy link
Author

mhenderson442 commented May 23, 2016

If it's useful, here are some the steps I follow trying to solve the issue:

  1. Make a change to project.json and save
  2. Go to cmd and hit dotnet restore
  3. If there are no errors or warnings, I hit dotnet ef

At this point I get the error you see at the start of this thread.

@lajones
Copy link
Contributor

lajones commented May 23, 2016

Unfortunately I think we are hitting #5320.

@mhenderson442
Copy link
Author

#5320 does look quite similar. I used the Web Api template to create the project. I even tried a Web Application template to see if something was not right with the template.

@divega
Copy link
Contributor

divega commented May 25, 2016

@natemcmaster do you think this in fact may be a dupe of #5320?

@natemcmaster
Copy link
Contributor

Depends. Is emitEntryPoint set to true? Also, what is the output when adding --verbose?

@mhenderson442
Copy link
Author

@natemcmaster

emitEntryPoint is set to true.

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

Here is the output when running Scaffold-DbContext with verbose:

Working directory: C:\Source\Repos\MercurySchool\MercurySchool\MercurySchoolApi
Executing command: dotnet ef --configuration Debug --build-base-path .\bin\ dbcontext scaffold 'Server=[Server named omitted];Data Source=[Data source omitted];Initial Catalog=MercurySchool;Persist Security Info=False;User ID=[User ID omitted];Password=[Password omitted];Pooling=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;' Microsoft.EntityFrameworkCore.SqlServer --context DbMercurySchool --output-dir DataModel --verbose
System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified. File name: 'Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
    at Microsoft.DotNet.ProjectModel.ProjectReader.ReadProject(Stream stream, String projectName, String projectPath, ProjectReaderSettings settings)
   at Microsoft.DotNet.ProjectModel.ProjectReader.GetProject(String projectPath, ProjectReaderSettings settings)
   at Microsoft.EntityFrameworkCore.Tools.DispatchCommand.<>c__DisplayClass2_0.<Create>b__0()
   at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
    at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)
 Could not load file or assembly 'Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified. 

@natemcmaster
Copy link
Contributor

natemcmaster commented May 26, 2016

@mhenderson442 thanks for that. I suspect this is related to imports. Try this to fix it

  1. Delete %USERPROFILE%\.nuget\packages\.tools\Microsoft.EntityFrameworkCore.Tools and %USERPROFILE%\.nuget\packages\Microsoft.EntityFrameworkCore.Tools. (cref https://github.com/dotnet/cli/issues/2978)
  2. Make sure your "tools" section matches this:

Preview 1

  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net451+win8"
    }

Preview 2

  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
    }

cref https://docs.efproject.net/en/latest/miscellaneous/cli/dotnet.html
3. Re run dotnet restore and look in the output for errors. VS will trigger this automatically when you save project.json and spits output to "Output > Package Manager".
4. If commands still fail, can you share the files in %USERPROFILE%\.nuget\packages\.tools\Microsoft.EntityFrameworkCore.Tools?

@mhenderson442
Copy link
Author

mhenderson442 commented May 26, 2016

@natemcmaster Following your instructions, dotnet ef worked.

Using Package Manager Console I got the following error:

Scaffold-DbContext : The term 'Scaffold-DbContext' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a 
path was included, verify that the path is correct and try again.
At line:1 char:1
+ Scaffold-DbContext -Provider "Microsoft.EntityFrameworkCore.SqlServer ...
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Scaffold-DbContext:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

command dotnet ef dbcontext scaffold executed with errors, but I think that's unrelated to this issue.

Got the following error: One for more errors ocurred. (Invalid object name 'sys.sequences'.).

I am connecting to a SQL Azure database. Is this supported?

@natemcmaster
Copy link
Contributor

👍 glad we got the first error out of the way.

dotnet ef dbcontext scaffold executed with errors, but I think that's unrelated to this issue.

Yes, can you open another issue? We support querying from SQL Azure, but I don't think we have ever testing scaffolding from a SQL Azure database. cc @lajones

This can probably be closed now. For anyone else who encounters this (cc @Kurira), the "Newtonsoft.Json" error is likely due to NuGet caching issues. Clear your caches as instructed above #5458 (comment)

@mhenderson442
Copy link
Author

mhenderson442 commented May 27, 2016

From my perspective I think this issue is closed. I will tinker some more to make sure the other errors warrant a new issue.

@lajones @divega @natemcmaster Thanks for the guidance.

@natemcmaster natemcmaster changed the title Scaffold-DbContext throwing System.IO.FileNotFoundException error. .NET Core CLI commands fail when wrong imports were used for first run of dotnet-restore Jun 8, 2016
@ole1986
Copy link

ole1986 commented Jul 19, 2016

Hello,

I came to a similar problem after fixing Could not load file or assembly 'Newtonsoft.Json issue.
Now I get: Could not load file or assembly 'System, Version=2.0.5.0 [...]

EDIT: Occurs when I am trying to execute Add-Migration from the Package Manager Console

My project.json

{
  "dependencies": {
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-rc2-final",
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Authentication": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Identity": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0-rc2-final"
  },

  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8"
    },
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    }
  },

  "frameworks": {
    "net461": {
      "frameworkAssemblies": {
      }
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

Any ideas?

@natemcmaster
Copy link
Contributor

@ole1986 as the issue describes, your imports are incorrect. For preview1, use "portable-net451+win8" See #5458 (comment)

@ajcvickers ajcvickers added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-unknown and removed closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-unknown labels Oct 15, 2022
@ajcvickers ajcvickers removed this from the 1.0.0 milestone Oct 18, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants