How To Update The Frameworks In Visual Studio For Mac



  1. How To Update The Frameworks In Visual Studio For Macbook Pro
  2. How To Update The Frameworks In Visual Studio For Mac Os

Entity Framework Core Migrations have changed once more thanks to Visual Studio 2017 and the .csproj format. In the third iteration of this series I once again show you how to enable migrations, this time including class libraries and multiple contexts. Bonus! There are examples for ASP.NET Identity and even Identity Server 4.

Entity Framework Core Migrations have changed once more thanks to Visual Studio 2017 and the.csproj format. In the third iteration of this series I once again show you how to enable migrations, this time including class libraries and multiple contexts. There are examples for ASP.NET Identity and even Identity Server 4.

  • These downloads may update Visual Studio and the.NET Framework on your machine. Some of the downloads may only support certain Visual Studio versions.NET Core.NET Core is a free, cross-platform, open-source developer platform for building many different types of applications.
  • Visual Studio for Mac is a powerful tool that allows users to build, test, monitor, and accelerate apps using C# for iOS, Android, and Windows phones. You can build native Android, iOS, and Windows apps across multiple platforms using native user interfaces. Visual Studio for Mac 8.5.2.13 comes with: Mono Framework MDK 6.8.0.99; Xamarin.iOS 13.
  • (Visual Studio) Update Azure Functions Runtime. Note: If you're using Visual Studio for Mac, skip to the next section. Let's now install the Azure Functions Runtime for Visual Studio 2019. In Visual Studio, select Create a new project; Visual Studio, Create New Project. In the Create a new project window, in the search bar, enter.

Starting from File > New Project

Visual

If you’re lucky enough to be starting a brand new project and it’s relatively small, Entity Framework Core migrations are already enabled for use when you select the “Individual Accounts” option from the file new project screen.

Starting from Scratch, Migrating from project.json or Using a Class Library

For the rest of us, especially those who appreciate good system architecture, we’re going to be setting up a separate class library to hold our migrations and Database Context. Luckily, since quite recently, the steps to enabling migrations have been simplified (Don’t worry there’s still planty to do). These steps are roughly the same, regardless if you’re upgrading to csproj project format or just creating a new class library.

1. Create a new .NET Standard Class Library

To hold our migrations and database context, we’ll create a .NET Standard Class Library. I typically use “[SolutionName].Data”.

2. Add some Nuget Packages

My example today will include ASP.NET Identity using Microsoft SQL Server, as I think this will cover the majority of use cases. If you don’t need Identity, don’t worry, you can just leave it out.

Start by adding the following Nuget Packages to your Data class library, using the Nuget Package Management screen or by editing the Data project’s .csproj file.

Note: Use the latest versions available to you at the current time.

These three packages add support for ASP.NET Identity using Entity Framework, EF Migrations tooling and EF SQL Server support respectively.

3. (Optional) Would you prefer to use the dotnet cli to create migrations? (like me)

Add the following code to your data project file (Right click project > “Edit [ProjectName]”).

4. Create your User Entity

Create a new folder called entities and add a new class to it called User.cs. I normally create a separate project to hold my entities called “[SolutionName].Domain”, but you can place this new class in your data project if you like. Here’s the code:

5. Create your Database Context

Add a new class to your data project called [SolutionName]Context.cs. I’m calling mine EFMigrations2017Context.cs. PS, if you’re not using ASP.NET Identity then you’d inherit from DbContext instead of IdentityDbContext<>. Here’s the code:

6. Create Temporary Database Context Factory

This is what let’s us self contain the class library, rather than relying on the configuration of the startup project. Whilst it’s a little bit of a pain to add this, it’s currently the most reliable way of ensuring migrations works for everyone.

Add the following TemporaryDbContextFactory.cs class to your data project:

How To Update The Frameworks In Visual Studio For Mac

7. Enable Migrations using Package Manager Console or Dotnet CLI

How

Package Manager Console
If you’d prefer to stay inside Visual Studio, you can enable migrations from the package manager console. Just run the following command (remembering to set the default project to your data project):

Dotnet CLI
If you prefer to keep a separate console window open to manage migrations, then open your terminal of choice in the directory of your data project and run the following command:

That’s it! Migrations are now enabled.

A migrations folder is added containing the new migrations.

Migrations Commands

Now that you have migrations enabled, here are the important commands you’ll need day to day.

Package Manager Console
You can find all commands listed here: https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell
This command adds a new migration based on the state of your DbContext.

This command removes the latest migration.
Important: Always use this command to remove a migration. Deleting a migration.cs file will result in a corrupted migrations model.

This command updates the database to the latest version. You can also optionally specify a target migration to migrate up or down to that migration.

Dotnet CLI
You can find all commands listed here: https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet
This command adds a new migration based on the state of your DbContext.

This command removes the latest migration.
Important: Always use this command to remove a migration. Deleting a migration.cs file will result in a corrupted migrations model.

This command updates the database to the latest version. You can also optionally specify a target migration to migrate up or down to that migration.

How do I run these migrations automatically?

If you’d like your application to automatically apply these migrations, much like the old DatabaseInitializers of EF 6, then here’s a quick snippet you can add to the startup.cs of your main application.

If you noticed, I also like to seed the database at this point as well as it will only run once per application startup. Be careful though, you cannot use async await here, as it buggers up the pipeline. Stick to using synchronous methods, or the .Wait() method on Tasks.

How To Update The Frameworks In Visual Studio For Macbook Pro

How do I use Multiple Contexts? / How do I set up Identity Server 4 to use Entity Framework?

Here’s a lovely bit of bonus content. The best way I can think of to demonstrate how to use multiple contexts, happens to be one of my most asked questions. How do I connect Identity Server 4 to Entity Framework and use migrations? Well! Let’s go through what you need to do to get up and running.

The following instructions will build upon the Context we’ve already created, using it as our main database. We’re also going to be using ASP.NET Identity as our User authentication system, so we’re keeping the IdentityUser and the IdentityDbContext<> intact.

The Web Project refers to our web application that we’ll be installing Identity Server 4 into.
The Data Project refers to the Data class library we created above.

1. To your Data Project, add the following Nuget Package:

Studio

How To Update The Frameworks In Visual Studio For Mac Os

2. Add the following to your TemporaryDbContextFactory.cs

I’m pointing the two Identity Server 4 Database Contexts at the same database as my main context so all data is in the same database. You can choose to split this up if you’d like.

3. Run Add Migrations for both contexts

Much like we did for our main context, this time we need to specify which context we want to use. I’m using the dotnet cli, but you can use the package manager if you like. See the reference for the correct command to use. Run the following commands from the data project folder:

You should now see two folders inside your migrations folder, containing the new migrations.

4. Set up Identity Server inside the Web Project startup.cs

I’m not going to go through the entire set up process of Identity Server 4, that is outside the scope of this post. Great samples already exist here: IdentityServer4.Samples/Quickstarts. Especially the AspNetIdentity and EntityFrameworkStorage projects.

Instead, once you’re setup with the in-memory example, here’s what you need to change to hook up Identity Server 4 to Entity Framework.

In your Web Projectstartup.cs, update the services configuration like so:

Visual

Here you can see I’m still using the DeveloperSigningCredential for this example. Be sure to use a certificate instead as soon as you want to deploy this.

Next you can see we’re hooking up the Configuration and Operational database contexts.

Finally we tell Identity Server 4 where we’re keeping our User information so it can hook into it automatically.

5. You’re good to go!

You now know mostly everything there is to know about using migrations. Until it changes again of course :P

If you have questions, hit me up on twitter: @BenWhoLikesBeer.