I see that there are to way how to scaffold entities and db context in Entity Framework Core 2.0.
using Scaffold-DbContext
using dotnet ef dbcontext scaffold
Why there are two tools and what is the difference?
Scaffold-DbContext runs in Visual Studio's NuGet Package Manager Console (PMC) and has better VS integration--opens files and infers the startup project.
dotnet ef dbcontext scaffold is a general command-line interface that can run outside of Visual Studio (and Windows).
Otherwise, they execute the exact same logic.
Related
I have .net Core 3.1 solution with WebApi and Persistance projects. WebApi is the solution's startup project, Persistance is where DbContext is defined. After a lot of reading and trying stuff, I still cannot achieve the following:
WebApi stays as the solution's startup project.
Migrations folder with all the migrations classes is in the Persistance project.
Migrations can be generated from VS Package Manager Console by typing Add-Migration <migration_name> without any additional parameters like -Project or -StartupProject.
No need to change selection in the Package Manager Console Default project dropdown.
I wanted to achieve the same result but on MAC OS. Because Visual Studio on Mac is not like Window's, We use CLI commands. In my case I had Migration Classes in another project and startup was Api project just like you so i added this to my terminal :
alias migrationfortest='dotnet ef migrations add $1 --project /Users/user/Projects/test-web/Test.Infrastructure'
and you can call this simply by typing migrationfortest "YOURMIGRATIONNAME"
NOTE : For CLI command tools you should add This
Is there an easy way to scaffold migrations using old EF outside of Visual Studio? I would like to do it via Rider IDE if it possible.
I don't see these options related to ef core. The plugin makes sense.
(For MacOS, be sure, your dotnet root folder path is /usr/local/share/dotnet/. I'm telling this because Rider installs and places it in another folder. Further it gives rise ef to not work properly. You can see your dotnet folder on terminal by writing which dotnet)
Installing that plugin,
Then,
You can also use a jetbrains plugin made for handling migrations
https://plugins.jetbrains.com/plugin/17026
For EF Core, you can use https://blog.jetbrains.com/dotnet/2017/08/09/running-entity-framework-core-commands-rider/
For EF 6 you may want to check https://blog.jetbrains.com/dotnet/2018/04/06/entity-framework-support-rider-2018-1/
The Package Manager Console tools such as Add-Migration, Scaffold-DbContext commands are PowerShell-based, and the Package Manager Console ties to several Visual Studio-specific objects making it impossible to host it elsewhere - in your case Rider.
In Rider's terminal or anywhere outside of Visual Studio, you can use CLI tools. Equivalents to the highlighted commands would be respectively:
Add-Migration => dotnet ef migrations add MigrationName
Scaffold-DbContext => dotnet ef dbcontext scaffold
You can get more details on JetBrains blog: Running EF Core commands in Rider
Trying to create a new console application in Visual Studio accessing an existing SQL Server database. Would like to use EF Core to scaffold models from the database.
MicroSoft's "Getting Started" instructions seem way off. They suggest using NuGet console and running
Scaffold-DbContext "Server=...." Microsoft.EntityFrameworkCore.SqlServer
This does nothing.
One source suggests running "dotnet ef dbcontext scaffold "server=....", but this errors out with:
CategoryInfo: NotSpecified: (No executable f...and "dotnet-ef":String) [], RemoteException
If I'm running dotnet ef shouldn't I been a command prompt, rather than PM console? But in which folder? Running in the project folder errors out with no executable found matching "dotnet-ef". Note that this is a console application (not ASP) and does not have Project.json, etc...
Did you install EF Nuget Package ?
Install-Package EntityFramework
https://www.nuget.org/packages/EntityFramework
How to run those dotnet.exe ef <command> commands programmatically in .NET Core?
For example to add migration I'm running in terminal dotnet ef migrations add NewMigration and it will indeed create Migrations folder with migration classes, but to create new Migration (for example) programmatically from C# code?
Don't suggest Process.Start("cmd bla-bla") since code should be cross-platform and that dotnet ef runs some code from some EntityFrameworkCore package anyway. Question is what code?
EF Core API isn't really designed for the scenario, but if you want to do this anyways, you'll need to repeat the logic that "dotnet-ef.dll" does to gather project context and compilation output, and then instantiate and use MigrationsOperations manually.
See https://github.com/aspnet/EntityFramework/blob/1.0.0/src/Microsoft.EntityFrameworkCore.Design.Core/Design/MigrationsOperations.cs and https://github.com/aspnet/EntityFramework/blob/1.0.0/src/Microsoft.EntityFrameworkCore.Design/Internal/OperationExecutor.cs
Use caution: these are "Internal" APIs, which means their usage may break from version to version. "dotnet ef" is going to change a great deal between the current release (1.0.0-preview2) and the next release. (For example, the entire tooling implementation will change. See https://github.com/aspnet/EntityFramework/issues/5334).
When I try to create a new migration using EF Code-First the package manager console gives me an error saying: 'No migrations configuration type was found in the assembly 'Project.DataAccess'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).'
I've previously used this method on THIS project successfully and the project do have a Configuration file with all the correct inheritance.
I also tried specifying all the parameters -Name, -StartupProjectName etc. but no change. When trying to re-enable migrations VS is not successful creating the Configuration with inheritance from my context-class.
Any suggestion on this issues? Using EF 4.3.1 and Code-First
This problem was resolved by installing EF 4.3.1 using the Package Manager Console. I had done a manual upgrade and for some reason that didn't do the trick.