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

Problem with database first scaffolding #7068

Closed
arruw opened this issue Nov 19, 2016 · 9 comments
Closed

Problem with database first scaffolding #7068

arruw opened this issue Nov 19, 2016 · 9 comments

Comments

@arruw
Copy link

arruw commented Nov 19, 2016

I'm having problem with scaffolding DbContext. I'm using latest .NET Core 1.1.0 SDK. I'm following this guide: https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db

But when I run following command in Package Manager Console: Scaffold-DbContext "<connection-string>" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models I get exception.

project.json

  "dependencies": {
    "Microsoft.EntityFrameworkCore": "1.1.0",
    "Microsoft.EntityFrameworkCore.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
    "NETStandard.Library": "1.6.1"
  },

  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final"
  },

  "frameworks": {
    "netstandard1.6": {
      "imports": [
        "dnxcore50"
      ]
    }
  }

Exception

System.IO.FileNotFoundException: Could not load file or assembly 
'System.Data.SqlClient, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
File name: 'System.Data.SqlClient, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.SqlServerDatabaseModelFactory.Create(String connectionString, TableSelectionSet tableSelectionSet)
   at Microsoft.EntityFrameworkCore.Scaffolding.RelationalScaffoldingModelFactory.Create(String connectionString, TableSelectionSet tableSelectionSet)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.SqlServerScaffoldingModelFactory.Create(String connectionString, TableSelectionSet tableSelectionSet)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineeringGenerator.GetMetadataModel(ReverseEngineeringConfiguration configuration)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineeringGenerator.GenerateAsync(ReverseEngineeringConfiguration configuration, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContextAsync(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable`1 schemas, IEnumerable`1 tables, Boolean useDataAnnotations, Boolean overwriteFiles, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.<ScaffoldContextImpl>d__22.MoveNext()
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source, Int32& length)
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass4_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Could not load file or assembly 'System.Data.SqlClient, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
@constantin-v
Copy link

constantin-v commented Nov 21, 2016

Same here when I try to do a Update-Database (in Code First)

Project.json

{
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.EntityFrameworkCore": "1.1.0",
    "Microsoft.EntityFrameworkCore.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0",
    "NETStandard.Library": "1.6.1",
    "MyProject.Data.Entities": "1.0.0-*",
    "System.Data.Common": "4.3.0",
    "System.Data.SqlClient": "4.3.0",
    "System.Runtime": "4.3.0"
  },

  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final"
  },

  "frameworks": {
    "netstandard1.5": {
      "imports": [
        "dnxcore50",
        "portable-net451+win8"
      ]
    }
  }
}

I also tried with "System.Data.SqlClient": "4.1.0" and "System.Data.SqlClient": "4.1.0.0"

Exception

System.IO.FileNotFoundException: Could not load file or assembly 'System.Data.SqlClient, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Le fichier sp├®cifi├® est introuvable.
File name: 'System.Data.SqlClient, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
   at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerConnection.CreateDbConnection()
   at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_1.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Could not load file or assembly 'System.Data.SqlClient, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Le fichier sp├®cifi├® est introuvable.

@rowanmiller rowanmiller added this to the 1.2.0 milestone Nov 21, 2016
@rowanmiller rowanmiller self-assigned this Nov 21, 2016
@constantin-v
Copy link

I solved the problem, I don't know how, but it's working.

Project.json

{
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.EntityFrameworkCore": "1.1.0",
    "Microsoft.EntityFrameworkCore.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0",
    "NETStandard.Library": "1.6.1",
    "MyProject.Data.Entities": "1.0.0-*",
    "System.Data.Common": "4.3.0",
    "System.Data.SqlClient": "4.3.0",
    "System.Runtime": "4.3.0"
  },

  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final"
  },

  "frameworks": {

    "netcoreapp1.0": {
      "imports": [
        "portable-net40+sl5+win8+wp8+wpa81",
        "portable-net45+win8+wp8+wpa81"
      ],
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0-*"
        }
      }
    }
  },
  "runtimes": {
    "win10-x64": {}
  }
}

And I also add this

namespace Core.Data
{
    public class TemporaryDbContextFactory : IDbContextFactory<MyContext>
    {
        public MyContext Create(DbContextFactoryOptions options)
        {
            var builder = new DbContextOptionsBuilder<MyContext>();
            builder.UseSqlServer(@"Server=MyConnextionString;Trusted_Connection=True;");
            return new MyContext(); //builder.Options
        }
    }
}

@rowanmiller
Copy link
Contributor

@matjazmav I spent some time debugging this and it is specific to class libraries. You can workaround it by using one of the workarounds listed at the end of https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet. The least intrusive is to add a console application to your project, that references your class library, and then specify it as the startup project.

I took your project.json for your class library, and then added a stub console app to the solution with the following project.json;

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "ClassLibrary1": "1.0.0",
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.1.0"
    }
  },

  "frameworks": {
    "netcoreapp1.1": {
      "imports": "dnxcore50"
    }
  }
}

This command then works for me, it successfully scaffolds the model into the class library:

PM> Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=AdventureWorks2014;Trusted_Connection=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -StartupProject ConsoleApp1

@rowanmiller
Copy link
Contributor

I suspect this is already covered by #5320, but will wait to confirm with @bricelam before closing.

@rowanmiller rowanmiller removed this from the 1.2.0 milestone Nov 22, 2016
@rowanmiller rowanmiller removed their assignment Nov 22, 2016
@rowanmiller rowanmiller added this to the 1.2.0 milestone Nov 22, 2016
@rowanmiller
Copy link
Contributor

@bricelam feel free to close if this is already tracked by #5320

@j0nscalet
Copy link

I can confirm this as well, as I just ran through the steps that @rowanmiller mentioned. I was using a class library before and created a console app instead to test this.

A quick note: @constantin-v project.json includes "System.Data.Common", "System.Data.SqlClient", and "System.Runtime" but those packages are not needed. (May be a side effect of his solution).

The following project.json file should resolve this and enable tool commands like 'Scaffold-DbContext' to run:

{

  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore": "1.1.0",
    "Microsoft.EntityFrameworkCore.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
    "NETStandard.Library": "1.6.1",
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.1.0"
    }
  },
  "frameworks": {
    "netcoreapp1.1": {
      "imports": "dnxcore50"
    }
  },
  "tools": {
    "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.0.0-preview3-final",
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  }
}

@bricelam
Copy link
Contributor

Closing as a dupe of #5320

@bricelam bricelam removed this from the 1.2.0 milestone Nov 29, 2016
@bricelam bricelam removed their assignment Nov 29, 2016
@LoopsLu
Copy link

LoopsLu commented Feb 8, 2017

I use this project.json. I think it is enough. I copied some settings from the WebApi project, and it works as well as the WebApi project in my Library project.

{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
},
"Microsoft.EntityFrameworkCore": "1.1.0",
"Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
"Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0",
"Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
"NETStandard.Library": "1.6.1"
},

"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
}
}

@Xamarians
Copy link

Xamarians commented May 11, 2017

I were having the same error. Fixed by making it default project ("Set as Startup Project").

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 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

8 participants