How to scaffold EF core to existing DB? - ef-code-first

I am trying to reverse engineer an existing database using Entity Framework Core. I tried to follow the directions from Microsoft but I am presented with the error:
Unable to find provider assembly with name EntityFramework. Ensure the specified name is correct and is referenced by the project.
I am running the following command from the project directory:
dnx ef dbcontext scaffold "Server=REMOVED;Database=REMOVED;User ID=REMOVED;Password=REMOVED" EntityFramework
What am I doing wrong?

Install fallowing NuGet package:
Install-Package Microsoft.EntityFrameworkCore.SqlServer

I was getting the following error when trying to scaffold from an existing SQLite database:
Unable to find provider assembly with name Microsoft.EntityFramworkCore.Sqlite. Ensure the specified name is correct and is referenced by the project.
The project was in a solution with one other project. Even though the correct project was referenced in the Package Manager Console it only worked when I selected the 'Set as Startup Project' item on the project context menu.

Make sure you are in the project folder and not the solution folder context. I was able to get this to work with the following yesterday (Notice the EntityFramework.MicrosoftSqlServer at the end)
dnx ef dbcontext scaffold "{connectionString}" EntityFramework.MicrosoftSqlServer
EDIT:
Make sure to include the following in your project.json:
EntityFramework.MicrosoftSqlServer.Design

Close Visual Studio
Delete %temp% folder
Delete .vs folder
dotnet ef dbcontext scaffold "Server=REMOVED;Database=REMOVED;User ID=REMOVED;Password=REMOVED" Microsoft.EntityFrameworkCore.SqlServer

Install Microsoft.EntityFrameworkCore.SqlServer from Nuget
In PM set the default project to the project that will contain the Models folder, then in PM...
PM> Scaffold-DbContext "Server=YOURSERVER;database=YOURDB;Trusted_Connection=True;" MicrosoftEntityFrameworkCore.SqlServer -OutputDir Models -force

Related

asp.net core 3.1 sqlite The specified module could not be found

I'm having some troubles with loading the mod_spatialite extension in sqlite in an ASP.NET Core 3.1 application. When I run the code below I get the following error: "SQL logic error The specified module could not be found."
string dbFile = "D:\\Temp\\spatialtest.db";
string connectString = $"Data Source={dbFile};Version=3;";
SQLiteConnection connection = new SQLiteConnection(connectString);
connection.Open();
connection.EnableExtensions(true);
connection.LoadExtension("mod_spatialite");
I reference the System.Data.Sqlite(https://www.nuget.org/packages/System.Data.SQLite/1.0.113.1) package from NuGet. The packages I use for loading in spatialite are found here http://www.gaia-gis.it/gaia-sins/. I tried the packages from the current stable 4.3.0a and the new 5.0.0-RC1, both the amd64 and x86 binaries.
The weird thing is that when I copy the code to an .NET Core Console application or to an .NET Core Class Library(CL) and then reference the CL from the console application it does work.
I don't get why it does work in an Console application and not in an ASP.NET application. Am I missing something? Any help is greatly appreciated!
EDIT: Added test project to Github: https://github.com/RogierB/SQLiteSpatialiteTest
Probably in console application you are using EF? With EF installation for SQLite will be installed all necessary libraries.
Try to add this library
Install-Package SQLitePCLRaw.bundle_e_sqlite3 -Version 2.0.3
I have found a working solution for my problem. I created a new folder and added all the files from the gaia download (http://www.gaia-gis.it/gaia-sins/) to that folder. The next thing I did was adding the newly created folder to the PATH environment variable for my system (Win 10). Restarted Visual Studio and now it does start and I don't even need to have the DLL's in the project/solution anymore because it will take the DLL's from the PATH

How to output SQLite database in a different project in EF Core 3

I have a solution with 2 projects
src
|-ProjectA
|-ProjectB
ProjectA is a class library targeting netstandard2.1, it contains the DbContext (with "Data Source=blog.db"), the entities and it has a dependency on Microsoft.EntityFrameworkCore.Sqlite
ProjectB targets a netcoreapp3.1 and has a dependency on Microsoft.EntityFrameworkCore.Design. I just use it to manage migrations because migrations need a framework.
The SQLite database does not exist, so I create the first migration with dotnet tools from the ProjectB
dotnet ef migrations add InitialCreate --project ../ProjectA
and it successfully creates the Migrations folder with the initial one.
Now I want to update the db so that the SQLite *.db is actually created.
If I do this:
dotnet ef database update --project ../ProjectA
the *.db file is created but in the ProjectB. I want the file to be output in the ProjectA but I cannot find a way to do so.
The doc does not seem to mention anything and neither the ef database update --help
UPDATE 1: I have tried to create and update migrations from the ProjectA instead and point to the executable project like this:
dotnet ef migrations add InitialCreate --startup-project ../ProjectB
and it creates properly the Migrations in ProjectA. Now I try to apply them to generate the *.db like this:
dotnet ef database update --startup-project ../ProjectB
and it generates the *.db file... but again in ProjectB! not in ProjectA where I want.
It seems wherever the Microsoft.EntityFrameworkCore.Design package is, the generated database is placed there.
UPDATE 2: Specifying the project when updating database to point to where the DbContext is, didn't work either. It keeps generating the *.db in Project B
dotnet ef database update --startup-project ../Sasw.SimpleBlog.Storage.SQLite.Migrator/ --project ./
I fixed it by specifying a full path at my connection string. It's sufficient because this path should be configurable in settings.
public class DatabaseContext
: DbContext
{
public DbSet<Thing> Things{ get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlite("Data Source=D:\\database.db");
}
}
The only problem left here is how to pass this connection string at runtime by using the dotnet ef CLI. But that seems an open question atm https://github.com/aspnet/EntityFrameworkCore/issues/10750
I know its kinda late for this response but maybe this helps, i haven't tried it.
Have you tried choosing a different default project in Visual Studio's Package Manager Console?

An assembly specified in the application dependencies manifest (<my-project>.deps.json) was not found

When I run:
dotnet ef migrations add InitialCreate
It errors:
Error:
An assembly specified in the application dependencies manifest (idb-dialog-flow.deps.json) was not found:
package: 'idb-dialog-flow', version: '1.0.0'
path: 'idb-dialog-flow.dll'
Here is a link to my project: https://github.com/islaam-database/chatbot.
How do I get this to work?
Edit: I forgot to mention that this project is an Azure Function.
I was able to find a workaround. I created a new project under the same solution. It was a library class project. I ran the entity framework commands in that project and it worked. I then imported that project into my first project and the DB calls were successful.

Can't scaffold with ef core for a console application project, doesnt reference .design package

When using Entity Framework Core for a console application i can't scaffold the database.
dotnet ef dbcontext scaffold "Server=;User Id=;Password=;Database="
"Microsoft.EntityFrameworkCore.SqlServer" -c Context -o Models -t Tables -f
Your startup project '' doesn't reference
Microsoft.EntityFrameworkCore.Design. This package is required for
the Entity Framework Core Tools to work.
Ensure your startup project is correct, install the package, and try again.
I've tried to install both Microsoft.EntityFrameworkCore.Design and Microsoft.EntityFrameworkCore.SqlServer.Design. Same problem.
If i set up a new mvc core application i don't get this error. Only when i set up new console applications.
What am i doing wrong?
I ran into a similar problem until I used the Entity Framework Core .NET CLI. Try the following to see if it will work for you:
dotnet new console
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet ef -h
You should see the EFC CLI Options screen. Do the following to scaffold (replace connection string with your own):
dotnet ef dbcontext scaffold "Server=.\;Database=AdventureWorksLT2012;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Model
Once you to that, you should see a Model folder in your project with the class files representing the entities.

Entity Framework Core 2.0 Scaffold-DbContext returns Build Error

In Visual Studio 2017, create a .NET core class library project with Target Framework as 2.0.
Have included below packages by Package Manager console.
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.SqlServer.Design
Microsoft.EntityFrameworkCore.Tools
Microsoft.VisualStudio.Web.CodeGeneration.Design
While running below Scaffold-DbContext command from Package Manager console of the same project, getting Build Failed error.
Scaffold-DbContext "Data Source=HTGHTFG135611L;Initial Catalog=HHH;Persist Security Info=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir DBModels -force -v
Also tried giving UserId and password, still the same error.
I had the same problem today, please be sure any project in your solution does not fail to build. Examine output folder after you got this error. I think one of your projects in your solution fails to build.

Resources