Enable Migrations Visual Studio For Mac
Installation
At some point, I have to enable EF migrations. The tutorial says: Go to View - Other Windows - Package Manager Console. Unfortunately there is no Package Manager Console in Visual Studio for Mac. So how do you handle things like enable-migrations, add-migration or update-database on the Mac? Visual Studio for Mac.NET. Enable Migrations Breaks MVC EF Scaffolding. Closed - Lower Priority visual studio 2017 version 15.7windows.
- Download Visual Studio Code for macOS.
- Double-click on the downloaded archive to expand the contents.
- Drag
Visual Studio Code.app
to theApplications
folder, making it available in theLaunchpad
. - Add VS Code to your Dock by right-clicking on the icon to bring up the context menu and choosing Options, Keep in Dock.
Launching from the command line
You can also run VS Code from the terminal by typing 'code' after adding it to the path:
- Launch VS Code.
- Open the Command Palette (F1) and type 'shell command' to find the Shell Command: Install 'code' command in PATH command.
- Restart the terminal for the new
$PATH
value to take effect. You'll be able to type 'code .' in any folder to start editing files in that folder.
Note: If you still have the old code
alias in your .bash_profile
(or equivalent) from an early VS Code version, remove it and replace it by executing the Shell Command: Install 'code' command in PATH command.
To manually add VS Code to your path, you can run the following commands:
Start a new terminal to pick up your .bash_profile
changes.
Note: The leading slash is required to prevent
$PATH
from expanding during the concatenation. Remove the leading slash if you want to run the export command directly in a terminal.
Touch Bar support
Out of the box VS Code adds actions to navigate in editor history as well as the full Debug tool bar to control the debugger on your Touch Bar:
Mojave privacy protections
After upgrading to macOS Mojave version, you may see dialogs saying 'Visual Studio Code would like to access your {calendar/contacts/photos}.' This is due to the new privacy protections in Mojave and is not specific to VS Code. The same dialogs may be displayed when running other applications as well. The dialog is shown once for each type of personal data and it is fine to choose Don't Allow since VS Code does not need access to those folders. You can read a more detailed explanation in this blog post.
Updates
VS Code ships monthly releases and supports auto-update when a new release is available. If you're prompted by VS Code, accept the newest update and it will get installed (you won't need to do anything else to get the latest bits).
Note: You can disable auto-update if you prefer to update VS Code on your own schedule.
Preferences menu
You can configure VS Code through settings, color themes, and custom keybindings and you will often see mention of the File > Preferences menu group. On a macOS, the Preferences menu group is under Code, not File.
Next steps
Once you have installed VS Code, these topics will help you learn more about VS Code:
- Additional Components - Learn how to install Git, Node.js, TypeScript, and tools like Yeoman.
- User Interface - A quick orientation around VS Code.
- User/Workspace Settings - Learn how to configure VS Code to your preferences settings.
Common questions
Unable to open on macOS Catalina
After downloading, when you try to open VS Code on macOS Catalina, you may see a message 'Visual Studio Code' can't be opened because Apple cannot check it for malicious software'. This is because VS Code is not currently notarized but VS Code will run successfully on macOS Catalina.
To workaround the notarization check, follow the instructions to Open a Mac app from an unidentified developer or from the Apple menu, go to System Preferences > Security & Privacy > General and choose Open Anyway.
Why do I see 'Visual Studio Code would like access to your calendar.'
If you are running macOS Mojave version, you may see dialogs saying 'Visual Studio Code would like to access your {calendar/contacts/photos}.' This is due to the new privacy protections in Mojave discussed above. It is fine to choose Don't Allow since VS Code does not need access to those folders.
VS Code fails to update
If VS Code doesn't update once it restarts, it might be set under quarantine by macOS. Follow the steps in this issue for resolution.
-->In this tutorial, you start using the EF Core migrations feature for managing data model changes. In later tutorials, you'll add more migrations as you change the data model.
In this tutorial, you:
- Learn about migrations
- Change the connection string
- Create an initial migration
- Examine Up and Down methods
- Learn about the data model snapshot
- Apply the migration
Prerequisites
About migrations
When you develop a new application, your data model changes frequently, and each time the model changes, it gets out of sync with the database. You started these tutorials by configuring the Entity Framework to create the database if it doesn't exist. Then each time you change the data model -- add, remove, or change entity classes or change your DbContext class -- you can delete the database and EF creates a new one that matches the model, and seeds it with test data.
This method of keeping the database in sync with the data model works well until you deploy the application to production. When the application is running in production it's usually storing data that you want to keep, and you don't want to lose everything each time you make a change such as adding a new column. The EF Core Migrations feature solves this problem by enabling EF to update the database schema instead of creating a new database.
To work with migrations, you can use the Package Manager Console (PMC) or the CLI. These tutorials show how to use CLI commands. Information about the PMC is at the end of this tutorial.
Change the connection string
In the appsettings.json file, change the name of the database in the connection string to ContosoUniversity2 or some other name that you haven't used on the computer you're using.
This change sets up the project so that the first migration will create a new database. This isn't required to get started with migrations, but you'll see later why it's a good idea.
Note
As an alternative to changing the database name, you can delete the database. Use SQL Server Object Explorer (SSOX) or the database drop
CLI command:
The following section explains how to run CLI commands.
Create an initial migration
Save your changes and build the project. Then open a command window and navigate to the project folder. Here's a quick way to do that:
In Solution Explorer, right-click the project and choose Open Folder in File Explorer from the context menu.
Enter 'cmd' in the address bar and press Enter.
Enter the following command in the command window:
Samsung external ssd. You see output like the following in the command window:
Note
If you see an error message No executable found matching command 'dotnet-ef', see this blog post for help troubleshooting.
If you see an error message 'cannot access the file .. ContosoUniversity.dll because it is being used by another process.', find the IIS Express icon in the Windows System Tray, and right-click it, then click ContosoUniversity > Stop Site.
Examine Up and Down methods
When you executed the migrations add
command, EF generated the code that will create the database from scratch. This code is in the Migrations folder, in the file named <timestamp>_InitialCreate.cs. The Up
method of the InitialCreate
class creates the database tables that correspond to the data model entity sets, and the Down
method deletes them, as shown in the following example.
Migrations calls the Up
method to implement the data model changes for a migration. When you enter a command to roll back the update, Migrations calls the Down
method.
This code is for the initial migration that was created when you entered the migrations add InitialCreate
command. The migration name parameter ('InitialCreate' in the example) is used for the file name and can be whatever you want. It's best to choose a word or phrase that summarizes what is being done in the migration. For example, you might name a later migration 'AddDepartmentTable'.
If you created the initial migration when the database already exists, the database creation code is generated but it doesn't have to run because the database already matches the data model. When you deploy the app to another environment where the database doesn't exist yet, this code will run to create your database, so it's a good idea to test it first. That's why you changed the name of the database in the connection string earlier -- so that migrations can create a new one from scratch.
The data model snapshot
Migrations creates a snapshot of the current database schema in Migrations/SchoolContextModelSnapshot.cs. When you add a migration, EF determines what changed by comparing the data model to the snapshot file.
Use the dotnet ef migrations remove command to remove a migration. dotnet ef migrations remove
deletes the migration and ensures the snapshot is correctly reset. If dotnet ef migrations remove
fails, use dotnet ef migrations remove -v
to get more information on the failure.
See EF Core Migrations in Team Environments for more information about how the snapshot file is used.
Apply the migration
In the command window, enter the following command to create the database and tables in it.
The output from the command is similar to the migrations add
command, except that you see logs for the SQL commands that set up the database. Most of the logs are omitted in the following sample output. If you prefer not to see this level of detail in log messages, you can change the log level in the appsettings.Development.json file. For more information, see Logging in .NET Core and ASP.NET Core.
Use SQL Server Object Explorer to inspect the database as you did in the first tutorial. You'll notice the addition of an __EFMigrationsHistory table that keeps track of which migrations have been applied to the database. View the data in that table and you'll see one row for the first migration. (The last log in the preceding CLI output example shows the INSERT statement that creates this row.)
Run the application to verify that everything still works the same as before.
Compare CLI and PMC
The EF tooling for managing migrations is available from .NET Core CLI commands or from PowerShell cmdlets in the Visual Studio Package Manager Console (PMC) window. This tutorial shows how to use the CLI, but you can use the PMC if you prefer.
The EF commands for the PMC commands are in the Microsoft.EntityFrameworkCore.Tools package. This package is included in the Microsoft.AspNetCore.App metapackage, so you don't need to add a package reference if your app has a package reference for Microsoft.AspNetCore.App
.
Important: This isn't the same package as the one you install for the CLI by editing the .csproj file. The name of this one ends in Tools
, unlike the CLI package name which ends in Tools.DotNet
.
For more information about the CLI commands, see .NET Core CLI.
For more information about the PMC commands, see Package Manager Console (Visual Studio).
Get the code
Next step
In this tutorial, you:
- Learned about migrations
- Learned about NuGet migration packages
- Changed the connection string
- Created an initial migration
- Examined Up and Down methods
- Learned about the data model snapshot
- Applied the migration
Advance to the next tutorial to begin looking at more advanced topics about expanding the data model. Along the way you'll create and apply additional migrations.