dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.83k stars 3.2k forks source link

Problem with database first scaffolding #7068

Closed arruw closed 2 years ago

arruw commented 8 years ago

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 commented 8 years ago

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.
constantin-v commented 8 years ago

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 commented 8 years ago

@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 commented 8 years ago

I suspect this is already covered by https://github.com/aspnet/EntityFramework/issues/5320, but will wait to confirm with @bricelam before closing.

rowanmiller commented 8 years ago

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

j0nscalet commented 8 years ago

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 commented 8 years ago

Closing as a dupe of #5320

LoopsLu commented 7 years ago

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 commented 7 years ago

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