Today, I sat down to create a DAL for one of our internal projects here and was rather surprised by what I was met with when I clicked the Data option in 'Add New Item' in Visual Studio. The ADO .Net Entity Data Model and EF Generators were completely gone. Why? Well... because Microsoft hasn't made them yet, but that didn't stop them from releasing .Net Core 3 or EF 6, of course. When has not having a fully baked product ever stopped Microsoft before, right? Who remembers Windows ME or Vista? 😆 I was shocked to see people on Stack Overflow and Microsoft alike recommending that people create a project targeting the standard .net framework, create their designer or code first classes, and then paste them into their .Net Core project. 😳 This post will walk you through the steps necessary to get EF 6.x installed in your .Net Core 3 application and how to scaffold your existing database, so you have data models and a database context that you can work with.Â
Adding EF to .Net Core 3
The good news is that adding EF to a Core 3.1.2 project is actually pretty simple. You simply need to complete the following steps:
- First, you want to make sure that you have the .Net Core SDK, which you may find here. *Note this is the x64 installer.
- Now that you have installed the SDK you will be able to install EF directly into your core project. To do this open up the package manager console in Visual Studio and issue the following command:
dotnet tool install --global dotnet-ef
- Next, you're going to need to add a couple of packages so that you can generate your models from an existing database or make updates in the future. You do that with the following commands in package manager console:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer dotnet add package Microsoft.EntityFrameworkCore.Design
- Last but not least, you'll most likely want to scaffold your existing database and build out your models and dbContext. To do that, you execute the following command:
dotnet ef dbcontext scaffold "Server=xxxxxx;Database=xxxxxx;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer ---output-dir Models
Voila! That's it. All of your models should be located in the Models directory off of the root within your project. As always, should you have any questions or need any help you can always reach out to us on our Contact Page. Until next time... Happy Coding!